By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,715 Members | 748 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.

radio button problems :(

P: 15
Hi all,
I'm using Python 2.5.1 and TkInter.
Hey here is the problem. Suppose I have 4 radio buttons (lets say values 1,2,3 and 4). They arranged in 2 columns (Each of the 2 columns has 2 radio buttons). I want to produce a specific action whenever I select a total of 2 radio buttons (and click on the "Submit" button), 1 from each column. Suppose say 1 and 3 are selected it should print 'a'. If 1 and 4, then print 'b' and similarly for radio button number 2. Any ideas? I think that 2 variables v1 and v2 for the 2 sets of radio buttons should be defined and the 'dct' command implemented though I'm stuck at this. However any other ideas will be great too.... Looking forward .... Thanks!
Dec 4 '07 #1
Share this Question
Share on Google+
2 Replies


P: 15
Hello all again...
Hey I managed to find a solution to the problem :)... but would welcome alternative suggestions. For public scrutiny the code is :

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *                                                       1
  2.  
  3. root = Tk()
  4.  
  5. v1=IntVar()
  6. v2=IntVar()
  7.  
  8. button1=Radiobutton(root, text="1", value=1, variable=v1)
  9. button2=Radiobutton(root, text="2", value=2, variable=v1)
  10. button3=Radiobutton(root, text="3", value=3, variable=v2)
  11. button4=Radiobutton(root, text="4", value=4, variable=v2)
  12.  
  13. button1.grid(row=1, column=0)
  14. button2.grid(row=2, column=0)
  15. button3.grid(row=1, column=1)
  16. button4.grid(row=2, column=1)
  17.  
  18. def dialog():
  19.     if v1.get()*v2.get()==3:
  20.         print 'a'
  21.     if v1.get()*v2.get()==4:
  22.         print 'b'
  23.     if v1.get()*v2.get()==6:
  24.         print 'c'
  25.     if v1.get()*v2.get()==8:
  26.         print 'd'
  27.  
  28. button=Button(root, text='Click', command=dialog)
  29. button.grid(row=3, column=0, pady=5, padx=10)
  30.  
  31. close=Button(root, text='Exit', command=root.quit)
  32. close.grid(row=4, column=0, pady=10)
  33.  
  34. root.title('Multi radio button!')
  35. root.mainloop()
  36.  
Dec 4 '07 #2

bartonc
Expert 5K+
P: 6,596
A "dispatch dictionary" may come in handy here:
Expand|Select|Wrap|Line Numbers
  1. >>> def funct1():
  2. ...     print 'a'
  3. ...     
  4. >>> def funct2():
  5. ...     print 'b'
  6. ...     
  7. >>> def funct2():
  8. ...     print 'b'
  9. ...     
  10. >>> def funct3():
  11. ...     print 'c'
  12. ...     
  13. >>> def funct4():
  14. ...     print 'd'
  15. ...     
  16. >>> dispatchDict = {2:funct1, 3:funct2, 6:funct3, 8:funct4}
  17. >>> actionCode = 3
  18. >>> dispatchDict[actionCode]()
  19. b
  20. >>> 
Dec 4 '07 #3

Post your reply

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