0

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!

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by woooee
0

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".

0

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()
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.