Reputation: 29
Is it possible to change the text on a button when it is pressed, even when there are lots of buttons using the same callback command?
button1 = Button(self, text="1", command=self.getPressed)
button2 = Button(self, text="2", command=self.getPressed)
button1.grid(row=0, column=0)
button2.grid(row=0, column=1)
def getPressed(self):
button.config(self, text="this has been pressed", state=DISABLED)
I know this code wouldn't work because button isn't a variable, but that is the kind of thing I had in mind for the callback. (I am using the tkinter module in python 3.7)
Upvotes: 0
Views: 953
Reputation: 72
I know this answer comes 4 years after the question but maybe someone will find this solution useful. What can be done is to use partial
function with a reference to the button and button.update(command=...)
to set the command after the button is created.
This way we avoid creating a separate list with buttons and I find it more clean. Also, additional arguments could be passed if needed.
from functools import partial
button1 = Button(self, text="1")
button1.update(command=partial(self.getPressed, button1))
button2 = Button(self, text="2")
button2.update(command=partial(self.getPressed, button2))
button1.grid(row=0, column=0)
button2.grid(row=0, column=1)
def getPressed(self, button):
button.config(self, text="this has been pressed", state=DISABLED)
Upvotes: 1
Reputation: 31
What I would do is use lambda to pass in values into the functions so that you can code which button is being pressed. Here is what it would look like if you use lambda in your buttons,
self.Button1 = Button(self, text="1", command=lambda: getPressed(1))
If you do this. You can define a method that will take in that argument and convert it into a string. If that value is equal to "1": do something to this. Else if value is equal to "2": do something to that button.
If I apply this knowledge to your work. It would look something like this.
button1 = Button(self, text="1", command=self.getPressed)
button2 = Button(self, text="2", command=self.getPressed)
button1.grid(row=0, column=0)
button2.grid(row=0, column=1)
def getPressed(self, number):
if(number == "1"):
button1.config(self, text="this button has been pressed", state=DISABLED)
elif(number == "2"):
button2.config(self, text="Button 2 has been pressed" state=DISABLED)
I hope you understand what I'm saying here. And if you do, please reply back on how well I explained this to you.
Upvotes: 0
Reputation: 41
button1 = Button(..., command=lambda widget="button1": DoSomething(widget))
you have to pass the widget reference into the callback function, you can do it like this:
import tkinter as tk
main = tk.Tk()
def getPressed(button):
tk.Button.config(button, text="this has been pressed", state = tk.DISABLED)
button1 = tk.Button(main, text="1", command=lambda widget='button1': getPressed(button1))
button2 = tk.Button(main, text="2", command=lambda widget = 'button2': getPressed(button2))
button1.grid(row=0, column=0)
button2.grid(row=0, column=1)
main.mainloop()
Upvotes: 1
Reputation: 7176
You can use lambda to pass the number of the button as parameter to the callback function:
command=lambda:self.getPressed(1)
and then use an if to determine which button was pressed. Or you could store the buttons in a list and then just pass the index to tha callback function.
Example without using class notation:
from tkinter import *
root = Tk()
def getPressed(no):
button_list[no].config(text="this has been pressed", state=DISABLED)
button_list = []
button_list.append(Button(text="1", command=lambda:getPressed(0)))
button_list.append(Button(text="2", command=lambda:getPressed(1)))
button_list[0].grid(row=0, column=0)
button_list[1].grid(row=0, column=1)
root.mainloop()
Upvotes: 0