Tkinter 条目小部件 .get 不起作用 [英] Tkinter entry widget .get doesn't work
问题描述
我对 python 相当陌生,目前正在从事一个学校项目,我的目标是创建一个可用于搜索数据文件的搜索栏,但是我正在努力使搜索栏正常工作.我正在使用 tkinter 条目小部件.当我调用 .get() 时,不会打印条目小部件中的字符串.这是我的代码...
I am fairly new to python and am currently working on a school project, my aim is to create a search bar that can be used to search a data file, however I am struggling to get the search bar to work correctly. I am using the tkinter entry widget. When I call .get(), the string in the entry widget is not printed. Here is my code...
from tkinter import *
def searchButton():
text = searched.get()
print (text)
def drawStatWindow():
global searched
statWindow = Tk()
statWindow.title("View Statistics")
statWindow.config(bg = "grey")
statWindow.geometry('800x900')
searched = StringVar()
searchBox = Entry(statWindow, textvariable = searched)
searchBox.place(x= 450, y=50, width = 200, height = 24)
enterButton = tkinter.Button(statWindow, text ="Enter", command =searchButton)
enterButton.config(height = 1, width = 4)
enterButton.place(x=652, y=50)
drawStatWindow()
当我在输入小部件中输入一个字符串并按下回车按钮时,没有任何反应.就像我说的,我不是很有经验,这是我的第一个项目,但是在阅读了 tkinter 条目小部件之后,我不明白为什么这不起作用.我使用的是python V3.4.0谢谢.
When I type a string into the entry widget and press the enter button, nothing happens. Like I say I am not very experienced and this is my first project, but after reading about the tkinter entry widgets I can't understand why this won't work. I am using python V3.4.0 Thanks.
推荐答案
你必须添加 mainloop()
因为 tkinter
需要它来运行.
You have to add mainloop()
because tkinter
needs it to run.
如果您在 IDLE 中运行使用 tkinter
的代码,则 IDLE 运行自己的 mainloop()
并且代码可以工作,但通常您必须添加 mainloop()
最后.
If you run code in IDLE which use tkinter
then IDLE runs own mainloop()
and code can work but normally you have to add mainloop()
at the end.
并且您必须删除 tkinter.Button
中的 tkinter
.
And you have to remove tkinter
in tkinter.Button
.
from tkinter import *
def searchButton():
text = searched.get()
print(text)
def drawStatWindow():
global searched
statWindow = Tk()
statWindow.title("View Statistics")
statWindow.config(bg="grey")
statWindow.geometry('800x900')
searched = StringVar()
searchBox = Entry(statWindow, textvariable=searched)
searchBox.place(x= 450, y=50, width=200, height=24)
# remove `tkinter` in `tkinter.Button`
enterButton = Button(statWindow, text="Enter", command=searchButton)
enterButton.config(height=1, width=4)
enterButton.place(x=652, y=50)
# add `mainloop()`
statWindow.mainloop()
drawStatWindow()
这篇关于Tkinter 条目小部件 .get 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!