In the code below, clicking the button should change the black text from Hello to Goodbye. But when I run the program, it immediately says Goodbye.
Because you are calling self.bpress
when you create the self.button1
button:
self.button1 = Button(self, text = "Click Me", width = 10, command = self.bpress())
# ^^
Simply remove the parenthesis and assign command
to the self.bpress
function object itself:
self.button1 = Button(self, text = "Click Me", width = 10, command = self.bpress)
For future reference:
If you want to send parameters to the function, simply add lambda:
Example for a button with the command callback()
:
yourButton = Tkinter.Button(root, text="Go!", command= lambda: callback(variableOne, variableTwo, variableThree)).pack()