Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Tkinter Text 위젯에서 입력을 받는 방법은 무엇입니까?

<시간/>

tkinter에서는 패키지를 사용하여 Text 속성을 사용하여 텍스트 위젯을 만들 수 있습니다. 그러나 GUI 응용 프로그램을 만드는 동안 텍스트 위젯에서 입력을 캡처해야 하는 경우가 있습니다.

.get()을 사용하여 텍스트 위젯에서 사용자로부터 입력을 얻을 수 있습니다. 방법. 처음에는 1.0에서 END까지 입력 범위를 지정하여 END까지 시작하고 끝나는 문자를 표시해야 합니다.

#Import tkinter library
from tkinter import *

#Create an instance of tkinter window or frame
win=Tk()
win.geometry("700x300")

def get_input():
   value=my_text_box.get("1.0","end-1c")
   print(value)

#Creating a text box widget
my_text_box=Text(win, height=5, width=40)
my_text_box.pack()

#Create a button for Comment
comment= Button(win, height=5, width=10, text="Comment", command=lambda: get_input())

#command=get_input() will wait for the key to press and displays the entered text
comment.pack()

win.mainloop()

출력

위의 코드를 실행하면 사용자의 입력을 수락하고 콘솔에 출력을 인쇄하는 텍스트 상자가 표시됩니다.

Tkinter Text 위젯에서 입력을 받는 방법은 무엇입니까?