下面的代码将显示一个小的Tkinter用户界面,但是我的按钮不会触发名为copy\u loop的循环。我已经试了好几个小时了,想不出怎么解决这个问题。任何帮助都将不胜感激。你知道吗
import tkinter as tk
import threading
class App():
def __init__(self, master):
self.isrunning = False
self.button1 = tk.Button(main, text='start')
self.button1.bind = ("<Button-1>", self.startrunning)
self.button1.pack()
self.button2 = tk.Button(main, text='stop')
self.button2.bind = ("<Button-1>", self.stoprunning)
self.button2.pack()
def startrunning(self, event):
self.isrunning = True
t = threading.Thread(target=self.copy_loop)
t.start()
def stoprunning(self, event):
self.isrunning = False
def copy_loop(self):
while self.isrunning:
print("Running...")
main = tk.Tk()
app = App(main)
main.mainloop()
在不应该有等号的地方有个等号。命令应为:
或者更整洁:
方法签名应该有
event=None
:或者更灵活:
bind
是函数,不是属性。您拥有:实际上,您正在覆盖实例上的
bind
函数。它现在是一个元组。你知道吗相反,请执行以下操作:
同样的
button2
。你知道吗相关问题 更多 >
编程相关推荐