Python Forum
[Tkinter] Programmatically creating buttons that remember their positions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Programmatically creating buttons that remember their positions
#4
You could also store the row and column as attributes in the Button instance
from functools import partial
from tkinter import *
root = Tk()
root.title(" ")
root.geometry("250x250+0+20")

def button_print(button):
    print(f"{button.row + 1}, {button.column + 1}")

buttons = []
for grid_row in range(3):
    button_row = []
    for grid_column in range(3):
        button = Button(root, text = f"{grid_row + 1} {grid_column + 1}", width = 4, height = 3)
        button.row = grid_row
        button.column = grid_column
        button.configure(command=partial(button_print, button))
        button.grid(row = grid_row, column = grid_column, padx = (10, 10), pady = (10, 10))
        button_row.append(button)
    buttons.append(button_row)

root.mainloop()
(Jun-21-2023, 06:29 PM)Clunk_Head Wrote: Is there a GUI alternative that is natively capable of achieving my goal?
I think all GUIs including tkinter allow you to attach client data to a widget and to retrieve this client data when a callback is invoked.
Clunk_Head likes this post
Reply


Messages In This Thread
RE: Programmatically creating buttons that remember their positions - by Gribouillis - Jun-21-2023, 06:54 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] How to add conversions and fix button positions. javesike1262 7 3,058 Jan-31-2021, 04:39 PM
Last Post: deanhystad
  Creating a frame with 4 command buttons Heyjoe 5 2,614 Aug-21-2020, 03:16 PM
Last Post: deanhystad
  GUI Tkinter Widget Positions punksnotdead 3 3,065 Jun-12-2019, 06:06 PM
Last Post: Yoriz
  A little idea to remember wxPython classes Sebastian_Adil 0 2,369 Mar-26-2018, 10:23 PM
Last Post: Sebastian_Adil
  Fill out form on webpage and post request programmatically ian 2 3,698 Jul-18-2017, 03:12 PM
Last Post: ian

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020