获取 Tkinter Entry 小组件的内容
Entry 小组件是一个基本的单行字符小组件,仅支持单行文本输入。可以通过初始化 Entry(parent, width) 构造函数来定义一个 Entry 小组件。
为了验证 Entry 小组件,我们可以使用 get() 方法,其结果是输入到 Entry 小组件中的字符。
让我们定义一个接受单行文本输入的 Entry 小组件,我们将在 Entry 小组件中打印输入的字符。
示例
#Import the required Libraries
from tkinter import *
from tkinter import ttk
#Create an instance of Tkinter frame
win = Tk()
#Set the geometry of Tkinter frame
win.geometry("750x250")
def get_content():
#Get the content of Entry Widget
print(entry.get())
#Create an entry widget
entry= Entry(win, width= 40)
entry.pack(pady= 20)
#Create a button to validate the entry widget
button= ttk.Button(win, text= "Get Content", command= get_content)
button.pack(pady=10)
win.mainloop()输出
运行上述代码将显示一个窗口,其中包含一个 Entry 小组件和一个用于获取 Entry 小组件内容的按钮。
现在,单击 "获取内容" 按钮以打印 Entry 小组件的内容。一旦我们单击按钮,它将输出如下内容,
Hello World!
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP