It's Willem
It's Willem

Reputation: 816

How to give an "argument" to my function in Python?

I am using Python 2.7 and Tkinter. I am trying to make a button change its own text when clicked. The code seems correct, but I keep encountering this error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1413, in __call__
    return self.func(*args)
TypeError: doStuff takes exactly 1 argument (0 given)

Here is my code:

def doStuff(event):
    button01.configure(command=doStuff2)
    button01.configure(text="Click me again!")

button01 = Button(root, text="Click me", command=doStuff)
button01.grid(row=8, column=6)

Where am I screwing up and how do I pass the needed argument to doStuff()?

Upvotes: 0

Views: 113

Answers (1)

TigerhawkT3
TigerhawkT3

Reputation: 49330

You defined the function as taking an event. What for? Have you also bound it to a keyboard key? A keyboard key, when pressed, will pass an event to whatever function it's bound to. However, the Button widget does not. If this function is only connected to that Button, simply remove the event from the function definition.

If you have bound the function to both this Button and a keyboard key (or mouse action, or something else that generates an event), give it a default argument with def doStuff(event=None):.

Upvotes: 2

Related Questions