Jun-21-2023, 05:09 PM
With a trick, you could use command with partial, to call the callback together with the instance of the button itself as the first argument.
Example:
Example:
from tkinter import Tk, Button from tkinter.messagebox import showinfo from itertools import product from functools import partial class Gui(Tk): def __init__(self): super().__init__() self.setup() self.selected: None | Button = None def setup(self): for number, (col, row) in enumerate(product(range(10), range(5))): button = Button(self, text=f"Button {number:02d}") # callback, which also submits the button innstance to the function button["command"] = partial(self.clicked, button) button.grid(row=row, column=col) def clicked(self, button: Button): print(button["text"]) if self.selected: self.selected, selected = None, self.selected grid1 = button.grid_info() grid2 = selected.grid_info() button.grid_configure(row=grid2["row"], column=grid2["column"]) selected.grid_configure(row=grid1["row"], column=grid1["column"]) showinfo( "Button position changed", f"{selected['text']} swapped with {button['text']}", ) else: self.selected = button if __name__ == "__main__": Gui().mainloop()
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!