How to determine which Button was pressed?

Viewed 82

I created 5 buttons. When users click the button, new window opens. I want the opened windows have different title. For example users click button1, the window that was opened name will be "Button_1". This is some part of my code.

button1= tk.Button(window,image=photo1,command=Calculations)
button2= tk.Button(window,image=photo2,command=Calculations)
button3= tk.Button(window,image=photo3,command=Calculations)
button4= tk.Button(window,image=photo4,command=Calculations)
button5= tk.Button(window,image=photo5,command=Calculations)

def Calculations():
    window_2 = tk.Toplevel()
    window_2.geometry("1000x1000")
    window_2.title("Button_1")

I think if I know which button is pressed, I think I can assign a variable and write the window_2.title() with the format() method. Is there a way to find the button that was pressed and assign it to a variable or another idea?

3 Answers

You pass the button and its name/index using functools.partial(<command>, *arguments).

from functools import partial

def Calculations(button, name):        
    window_2 = tk.Toplevel()
    window_2.geometry("1000x1000")
    window_2.title(name)

# Create the button without a command or an empty command
button1= tk.Button(window, image=photo1)
# Configure the command later so that we can also pass in `button1` to the function
button1.config(command=partial(Calculations, button1, "button1"))


button2 = tk.Button(window, image=photo2)
button2.config(command=partial(Calculations, button2, "button2"))

...

You can use a lambda function for the button's command argument.

button1= tk.Button(window,image=photo1,command=lambda: Calculations("1"))    
button2= tk.Button(window,image=photo2,command=lambda: Calculations("2"))    
button3= tk.Button(window,image=photo3,command=lambda: Calculations("3"))    
button4= tk.Button(window,image=photo4,command=lambda: Calculations("4"))    
button5= tk.Button(window,image=photo5,command=lambda: Calculations("5"))

def Calculations(num):        
   window = tk.Toplevel()
   window.geometry("1000x1000")
   window.title("Button_" + num)

A more efficient solution would be to use a for loop:

for i in range(5):
    i += 1
    globals()["button" + str(i)] = tk.Button(window, image=globals()["photo" + str(i)], command=lambda i=i: Calculations(str(i)))    

def Calculations(num):        
   window = tk.Toplevel()
   window.geometry("1000x1000")
   window.title("Button_" + num)
button1= tk.Button(window,image=photo1,command=lambda: Calculations("button one"))    
button2= tk.Button(window,image=photo2,command=lambda: Calculations("button two"))    
button3= tk.Button(window,image=photo3,command=lambda: Calculations("button three"))    
button4= tk.Button(window,image=photo4,command=lambda: Calculations("button four"))    
button5= tk.Button(window,image=photo5,command=lambda: Calculations("button 5"))

so then when the button is pressed it will give a str variable which says which button was pressed. Example: "button one"

def Calculations(button):        
    window = tk.Toplevel()
    window.geometry("1000x1000")
    window.title(str(button))

then the title of the window will be which button was pressed.

you could also make a set of radio buttons/ check buttons and assign them to tk.IntVar() and make a unique value for each of them and make a 'done' button. ( it does the same thing expect that it will be check buttons)

   var = tk.IntVar()

    C1 = tk.Checkbutton(frame, onvalue=1, variable=self.var)
    C2 = tk.Checkbutton(frame, onvalue=2, variable=self.var)
    C3 = tk.Checkbutton(frame, onvalue=3, variable=self.var)
    C4 = tk.Checkbutton(frame, onvalue=4, variable=self.var)

done = tk.button(frame, command=calculations)

def Calculations(num):        
    window = tk.Toplevel()
    window.geometry("1000x1000")
    window.title("Button_" + var)
Related