[Tutor] Python 3 and tkinter Radiobuttons
Dave Angel
davea at ieee.org
Sun Oct 11 13:00:57 CEST 2009
(Don't top-post. It confuses everything. Put your reply at the bottom,
or maybe inline)
bob smith wrote:
>
> Thanks for the reply. Unfortunately, even when I include a variable, all of the buttons start out selected. I noticed that this is the case when I create a StringVar (rather than an IntVar, where the buttons start out correctly unselected). So, here's another simple example where all of the radio buttons start out incorrectly selected:
>
> from tkinter import *
>
> root = Tk()
> root.grid()
>
> v = StringVar()
> Radiobutton(root, text = "Test RadioButton 1", variable=v, value="1").grid(row = 0, column = 0, sticky = W)
> Radiobutton(root, text = "Test RadioButton 2", variable=v, value="2").grid(row = 1, column = 0, sticky = W)
>
> root.mainloop()
>
>
> Any ideas on how to have a StringVar() associated with a group of Radiobutton objects where all of the radio buttons start off unselected?
>
> --Bob
>
>
>
>> Date: Thu, 8 Oct 2009 20:43:21 -0400
>> Subject: Re: [Tutor] Python 3 and tkinter Radiobuttons
>> From: kent37 at tds.net
>> To: bobsmith327 at hotmail.com
>> CC: tutor at python.org
>>
>> On Thu, Oct 8, 2009 at 6:04 PM, bob smith <bobsmith327 at hotmail.com> wrote:
>>
>>> Hi. I’m using Tkinter to create a new Radiobutton in Python 3. However,
>>> when I create the button, it starts off looking selected instead of
>>> unselected (though it behaves correctly like an unselected Radiobutton). So
>>> this means when I create a group of Radiobuttons they all look selected when
>>> my program begins.
>>>
>> You have to associate the Radiobuttons with a variable, for example:
>>
>> from tkinter import *
>>
>> root = Tk()
>> root.grid()
>> v = IntVar()
>> button = Radiobutton(root, text = "Test RadioButton", variable=v, value=1)
>> button.grid(row = 0, column = 0, sticky = W)
>>
>> button = Radiobutton(root, text = "Test RadioButton2", variable=v, value=2)
>> button.grid(row = 1, column = 0, sticky = W)
>>
>> root.mainloop()
>>
>>
>> Kent
>>
> <snip>
I think Kent forgot to include the v.set(), to set the initial state of
that group of radiobuttons. To go to your example, just add one line:
from tkinter import *
root = Tk()
root.grid()
v = StringVar()
v.set("1")
Radiobutton(root, text = "Test RadioButton 1", variable=v,
value="1").grid(row = 0, column = 0, sticky = W)
Radiobutton(root, text = "Test RadioButton 2", variable=v,
value="2").grid(row = 1, column = 0, sticky = W)
root.mainloop()
This will select button 1, and leave button 2 unselected.
DaveA
More information about the Tutor
mailing list