只需点击一下按钮即可运行该程序。我试图在点击时使该按钮被禁用,并在5秒钟后激活,同时不干扰程序的其余部分。 (程序的其余部分在代码中称为#,其余程序运行)
import time
from tkinter import Tk, Button, SUNKEN, RAISED
from threading import Thread
def tFunc(button):
thread = Thread(target= buttonDisable, args=(button))
thread.start()
# here the rest of the program runs
def buttonDisable(button):
button.config(state='disable',relief=SUNKEN)
time.sleep(5)
button.config(state='active', relief=RAISED)
root = Tk()
button = Button(root, text='Button', command= lambda : tFunc(button))
button.pack()
root.mainloop()
但是我收到以下错误:
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Python33\lib\threading.py", line 637, in _bootstrap_inner
self.run()
File "C:\Python33\lib\threading.py", line 594, in run
self._target(*self._args, **self._kwargs)
TypeError: buttonDisable() argument after * must be a sequence, not Button
正如错误所说:func args期望序列而不是按钮对象。我如何解决这个问题?
答案 0 :(得分:5)
您应该将线程回调函数参数作为元组或列表传递:
thread = Thread(target= buttonDisable, args=(button,))
BTW,使用after
,您不需要使用线程。
import time
from tkinter import Tk, Button, SUNKEN, RAISED
def tFunc(button):
button.config(state='disable',relief=SUNKEN)
root.after(5000, lambda: button.config(state='active', relief=RAISED))
# Invoke the lambda function in 5000 ms (5 seconds)
root = Tk()
button = Button(root, text='Button', command= lambda : tFunc(button))
button.pack()
root.mainloop()