[Tkinter-discuss] help needed with radiobutton

Mikael Olofsson mikael at isy.liu.se
Tue Mar 27 16:51:42 CEST 2007


Sang Park wrote:
> how do I select radionbutton by default? 
> for my school project, I need to have 10 radio buttons and have half 
> of them selected
> I have
> for i in range(10):
> x = IntVar()
> if i < 5:
> rb = Radiobutton(buttonFrame, variable=x, value=1,state=DISABLED)
> else:
> rb = Radiobutton(buttonFrame, variable=x, value=0,state=DISABLED)
>
Your problem is that you throw away your IntVar-s after each round in the loop. Instead keep references to them around as in this example, which is a simple rewrite of your example.

>>> from Tkinter import *
>>> root=Tk()
>>> x=[]
>>> for i in range(10):
    x.append(IntVar())
    if i < 5:
        rb = Radiobutton(root, variable=x[-1], value=1, state=DISABLED)
    else:
        rb = Radiobutton(root, variable=x[-1], value=0, state=DISABLED)
    rb.pack()

    
>>> root.mainloop()

By the way, are you sure you really want state=DISABLED? That makes the radiobuttons disabled...

/MiO



More information about the Tkinter-discuss mailing list