Python tkinter detecting which button is pressed

Thread Starter

zazas321

Joined Nov 29, 2015
856
Hey. I am making a tkinter application and I cannot figure out how to handle which button is pressed. I have a for loop which automatially creates a selected number of entry and button widgets:
Code:
for x in range(0,len(self.Serial_order)):# CREATE A LIST OF ARRAYS BASED ON CURRENT CSV FILE AND DISPLAY
                self.serial_entries.append(tk.StringVar())
                self.serial_entries[x].set(self.Serial_order[x])
                
                self.order_entries.append(tk.StringVar())
                self.order_entries[x].set(self.Picking_order[x])
                self.delete_entry_button.append(tk.Button(self.innercanvas,foreground="green",text="Delete entry",command =lambda: self.delete_row_of_entry(x)))

                
                code_labels_entry = tk.Entry(self.innercanvas,width=5,textvariable=self.serial_entries[x])
                order_labels_entry = tk.Entry(self.innercanvas,width=5,textvariable=self.order_entries[x])
                self.innercanvas.create_window(70,100+(x*50),window=code_labels_entry)
                self.innercanvas.create_window(250,100+(x*50),window=order_labels_entry)
                #order_button = tk.Button(self.innercanvas,foreground="green",text="Patvirtinti",command =lambda: self.confirm_order_changes())
                self.innercanvas.create_window(350,100+(x*50),window=self.delete_entry_button[x])
The important part from the code is line:
Code:
                self.delete_entry_button.append(tk.Button(self.innercanvas,foreground="green",text="Delete entry",command =lambda: self.delete_row_of_entry(x)))
Here I am creating a tkinter Button widget and appending it to a list because I will need to access it later. Every button will execute the same command:
Code:
self.delete_row_of_entry(x)
Code:
    def delete_row_of_entry(self,event):
        print("event=",event)
        #APPEND PICKING ORDER AND CREATE A NEW ENTRY
As you can see, to a command function I pass variable x which is my for loop counter variable. So I was expecting Button1 to pass variable 1, Button2 - 2 and so on.

For example, size of for loop is 4, so any button pressed returns me value of 3 (the last value of for loop)
2020-11-26-081730_1920x1080_scrot.png

How can I create a button widget so that every button would return me a different value based on whihc button is pressed?
 

Attachments

Top