Is there a convenient (intrinsic) way to access to attributes of radiobutton (tkinter, python)?
I did serious search not only on Stackoverflow but also internet, many similar questions but the answers seem to mean that the only choice is to deal with "variable" parameter, which gives only value of selected button. I would like to access parameters of multiple buttons, example:
I am hoping that we have quick access such as radio_button.value, radio_button.text...
You can iterate over a list of the radiobutton objects themselves, and use cget
or configure
to get the values of any of the widget options.
The following example will print out the text and a boolean to show if it is selected or not.
import tkinter as tk
root = tk.Tk()
radios = []
radiovar = tk.IntVar(root, value=1)
for i in range(10):
radio = tk.Radiobutton(root, text=f"Choice #{i+1:2}", variable=radiovar, value=i+1)
radios.append(radio)
radio.pack()
for radio in radios:
is_selected = (radio.cget("value") == radiovar.get())
print(f"{radio.cget('text')}: selected? {is_selected}")
tk.mainloop()