tkinter - 通过自己的点击按钮来禁用

Pho*_*nix 0 python parameters tkinter python-multithreading python-3.x

只需单击一个按钮即可运行该程序.我试图使该按钮在被点击时被禁用,并在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()
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

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
Run Code Online (Sandbox Code Playgroud)

正如错误所说:func args期望序列而不是按钮对象.我怎么绕这个?

fal*_*tru 5

您应该将线程回调函数参数作为元组或列表传递:

thread = Thread(target= buttonDisable, args=(button,))
Run Code Online (Sandbox Code Playgroud)

顺便说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()
Run Code Online (Sandbox Code Playgroud)