如何将变量连接到 Tkinter Entry 控件?
Tkinter Entry 控件是一个支持和接受单行用户输入的输入控件。它接受 UTF-8 模块中的所有类型字符。为了从 Entry 控件获取输入,我们必须定义一个(基于它接受的数据类型)仅接受字符串字符的变量。然后,通过使用 get() 方法,我们可以打印来自 Entry 控件的给定输入。
示例
# Import the Tkinter Library
from tkinter import *
# Create an instance of Tkinter Frame
win = Tk()
# Set the geometry of window
win.geometry("700x250")
# Define a String Variable
var = StringVar()
# Define a function to print the Entry widget Input
def printinput(*args):
print(var.get())
# Create an Entry widget
entry = Entry(win, width=35, textvariable=var)
entry.pack()
# Trace the Input from Entry widget
var.trace("w", printinput)
win.mainloop()输出
运行上述代码将显示一个带有 Entry 控件的窗口。

当我们在 Entry 控件中输入内容时,它只会将 Entry 控件中的所有字符打印到控制台上。
H He Hel Hell Hello Hello Hello W Hello Wo Hello Wor Hello Worl Hello World Hello World!
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP