Hi!
I've been working with radio buttons and I've been having some trouble. The basic problem is that even though I can create the radio buttons (and I can select which one I want) that they don't do anything- nothing is printed or tells me what the "user" selected.
Here's the code I'm working on:


from Tkinter import *
root = Tk()

output = file('robot_info.txt', 'a')

def check():
print type1

v=StringVar()
COLOR = [("Blue", "Blue"), ("Red", "Red"), ("White", "White")]
v.set("Blue")

for text, type1 in COLOR:
b = Radiobutton(root, text = text, variable = v, value = type1, indicatoron=1, command=check)
b.pack(anchor = W)

root.mainloop()

#End of program

Thanks for all your help!

So it turns out that one of my co-workers figured it out! My function should look like this:
def check():
print v.get()
So it works now, but I'm still wondering about how to link pages to that- so if someone picks "Blue"- I'd like to have certain questions that are different from questions that would come up if you selected "Red" or "White".

I don't use Tkinter but it doesn't print because the function "check()" has no idea what type1 is. Run the code below. I have changed check() so that it just prints "type1". It now knows what to print and so you get some output. I think you have to use a control variable in Tkinter to get the state of a radio buttion(s). If you are going to be passing variables around in a Tkinter program, I would suggest using a class.

from Tkinter import *
root = Tk()
root.geometry("120x100+15+15")

output = file('robot_info.txt', 'a')

def check():
   print "type1"
 
v=StringVar()
COLOR = [("Blue", "Blue"), ("Red", "Red"), ("White", "White")]
v.set("Blue")

for text, type1 in COLOR:
   b = Radiobutton(root, text = text, variable = v, value = type1, indicatoron=1,   command=check)
   b.pack(anchor = W)

root.mainloop()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.