如何从 Tkinter 文本小工具获取输入?
在 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()输出
运行上述代码会显示一个文本框,该文本框将接受用户的输入,然后将输出打印到控制台上。

广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP