Donatello
Donatello

Reputation: 33

Tkinter: retrieve Radiobutton column and row value

Here How to retrieve the row and column information of a button and use this to alter its settings in python I've found a nice code that I would like to alter and use.

Here's the original:

from tkinter import *

root = Tk()

def showGrid():
    row    = btn.grid_info()['row']      # Row of the button
    column = btn.grid_info()['column']   # grid_info will return dictionary with all grid elements (row, column, ipadx, ipday, sticky, rowspan and columnspan)
    print("Grid position of 'btn': {} {}".format(row, column))

btn = Button(root, text = 'Click me!', command = showGrid)
btn.grid(row = 0, column = 0)

root.mainloop()

If the buttons are 2, how the def would recognize which button I've clicked? I've tried to change the button into a Radiobutton (which is fine for me), set a value, set a variable, ecc, tried to used .get() and so on but I am unable to clearly identify the single radiobutton.

Any suggestion?

Best

Upvotes: 0

Views: 225

Answers (1)

user15801675
user15801675

Reputation:

Using lambda might help.

from tkinter import *

root = Tk()

def showGrid(widget):
    row    = widget.grid_info()['row']      # Row of the button
    column = widget.grid_info()['column']   # grid_info will return dictionary with all grid elements (row, column, ipadx, ipday, sticky, rowspan and columnspan)
    print("Grid position of 'btn': {} {}".format(row, column))

btn = Button(root, text = 'Click me!', command = lambda: showGrid(btn))
btn.grid(row = 0, column = 0)
btn1 = Button(root, text = 'Click me!', command = lambda: showGrid(btn1))
btn1.grid(row = 1, column = 0)


root.mainloop()

Upvotes: 1

Related Questions