Question

The program runs upon the click of a button. I'm trying to cause that button, upon being clicked, to be disabled and 5 seconds later activate while not interfering with the rest of the program. (the rest of the program is referred to in the code as # here the rest of the program runs)

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()

But I get the following error:

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

As error says: func args expect sequences rather than button objects. How do I get round this?

Was it helpful?

Solution

You should pass thread callback function arguments as a tuple or a list:

thread = Thread(target= buttonDisable, args=(button,))

BTW, using after, you don't need to use thread.

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()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top