Reputation: 1
I want to be able to retrieve the text value from a radio button.
For example:
first = Radiobutton(frame, text=name, variable=V, value=0)
second = Radiobutton(frame, text=name, variable=V, value=1)
third = Radiobutton(frame, text=name, variable=V, value=3)
buttonPressed = V.get()
This will give me the value, but I am trying to figure out how to get the text value. Because my button are created from a generic list of attributes from an XML doc.
Upvotes: 0
Views: 4169
Reputation: 5844
You don't need to use an IntVar
, use a StringVar
instead:
>>> root=tkinter.Tk()
>>> V=tkinter.StringVar()
>>> name = "abc"
>>> first = tkinter.Radiobutton(root, text=name, variable=V, value=name)
>>> first.pack()
>>> V.get()
'abc'
>>>
That way, V.get()
is the same as the text
for the selected Radiobutton
.
The other way is to make an list of the Radiobutton
s, like [first, second, third]
. Then you can find the text
by radiobuttons[V.get()]["text"]
, where the variable V
is initialized by IntVar()
(V = IntVar()
), as the Radiobutton
can be accessed like a dictionary (equivalent to config
) to retrieve the text
value. However, this does involve maintaining the list.
Upvotes: 1
Reputation: 385970
To get the value of an attribute of a widget, use the cget
method:
first_text = first.cget("text")
Upvotes: 1