[Tutor] Tkinter radio buttons

Alan Gauld alan.gauld@freenet.co.uk
Sat, 25 Dec 1999 22:52:33 +0000


I tried a simple radiobutton app but the suggested 
behaviour didn't happen. I needed to add a command 
function. Is this normal in Tkinter or am I missing 
something?

The code below doesn't work, but will if you 
uncomment the commented out lines. I thought Tk 
should update the variable automagically...?

Alan G.

--------------------------------------
from Tkinter import *

rv=0

def DoIt():
    print rv
    
#def doZero():
#    global rv 
#    rv = 0

#def doOne():
#    global rv
#    rv = 1

app = Frame()
r1 = Radiobutton(app, 
                 variable=rv, value=0, 
                 text="Zero", # command=doZero
		     )
r1.pack()
r2 = Radiobutton(app, 
                 variable=rv, value=1, 
		     text="One", # command=doOne
		     )
r2.pack() 
r1.select()

b = Button(app, text="Showme", command=DoIt)
b.pack()

app.pack()
app.mainloop()