【发布时间】:2021-09-28 15:52:39
【问题描述】:
我正在尝试在 Tkinter 中创建一个按钮,该按钮仅在它在文本框中找到单词“hello”时才会出现(变得可见)。
我可以想象使用线程和全局变量,但我不知道如何编码, 我想象过类似的东西:
import Tkinter as *
from threading import Thread
window = Tk()
active = True
def check_for_word():
global active
textbox1 = textbox.get("1,0", "end")
while active == True:
if "hello" in textbox1:
button.pack()
else:
button.pack_forget()
save_button = Button(window)
textbox = scrolledtext.ScrolledText(window)
textbox.pack()
threading = Thread (target=check_for_word)
threading.start()
window.mainloop()
这是我怀疑可以工作但最终不行的东西,按钮要么根本不显示,就像代码甚至没有运行一样,要么线程无法正常工作。所以我做错了什么,如果是,你能帮帮我吗?谢谢!
【问题讨论】:
-
"1,0"应该是"1.0",你为什么使用 Python 2.x?这是一个不推荐使用的版本,不应该使用,第二:我强烈建议在导入某些东西时不要使用通配符 (*),你应该导入你需要的东西,例如from module import Class1, func_1, var_2等等或导入整个模块:import module然后你也可以使用别名:import module as md或类似的东西,关键是不要导入所有东西,除非你真的知道你在做什么;名称冲突是问题所在。当你打包按钮集active = False并且不需要它是全局的 -
使用
threading和tkinter不是一个好主意。有时tkinter从另一个线程调用它可能会崩溃。
标签: python multithreading tkinter button find