By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,715 Members | 768 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,715 IT Pros & Developers. It's quick & easy.

tkinter, radiobuttons, variables

P: 3
This is my code. I've tried a trillion different things. But basically what I want is the radiobuttons to appear. Select one and hit the okay button. Then the screen prints out the var of which button was selected (just to verify). And I want the whole program to wait and do nothing until this process is done. (What I'd want to do next is another radiobutton set, but I'm not there yet.)

Help! I've been researching this online all day today. I've found tons of posts with people with the same problems, but either the solutions weren't available, or made no sense to me.

Thank you very much in advance.





from Tkinter import *

master = Tk()

var = IntVar()

Radiobutton(master, text="One", variable=var, value=1).pack(anchor=W)
Radiobutton(master, text="Two", variable=var, value=2).pack(anchor=W)

Button(master, text='OK!',command=master.destroy).pack()

print "moo1 var = ",var
master.wait_variable
print "moo2 var = ",var

master.mainloop()
Oct 6 '06 #1
Share this Question
Share on Google+
1 Reply


bartonc
Expert 5K+
P: 6,596
I future, read posting or reply guidelines to learn how to use CODE tags so you post looks like this:

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2.  
  3. master = Tk()
  4.  
  5. var = IntVar()
  6. var.set(1)  # Set the defalt value to something
  7.  
  8. # I like to keep the instanciated button around in case I want to config it later
  9. rb1 = Radiobutton(master, text="One", variable=var, value=1)
  10. rb2 = Radiobutton(master, text="Two", variable=var, value=2)
  11. rb1.pack(anchor=W)
  12. rb2.pack(anchor=W)
  13.  
  14. #This should be what you needed.
  15. def print_rb_state():
  16.     print "var = ", var.get()
  17.  
  18. Button(master, text='Print State!',command=print_rb_state).pack()
  19.  
  20.  
  21. master.mainloop()
  22.  
Oct 10 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.