radio button problems :(

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vajratkarviraj
    New Member
    • Jun 2007
    • 15

    radio button problems :(

    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!
  • vajratkarviraj
    New Member
    • Jun 2007
    • 15

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

    Code:
    from Tkinter import *                                                       1
        
    root = Tk()
    
    v1=IntVar()
    v2=IntVar()
    
    button1=Radiobutton(root, text="1", value=1, variable=v1)
    button2=Radiobutton(root, text="2", value=2, variable=v1)
    button3=Radiobutton(root, text="3", value=3, variable=v2)
    button4=Radiobutton(root, text="4", value=4, variable=v2)
    
    button1.grid(row=1, column=0)
    button2.grid(row=2, column=0)
    button3.grid(row=1, column=1)
    button4.grid(row=2, column=1)
    
    def dialog():
        if v1.get()*v2.get()==3:
            print 'a'
        if v1.get()*v2.get()==4:
            print 'b'
        if v1.get()*v2.get()==6:
            print 'c'
        if v1.get()*v2.get()==8:
            print 'd'
    
    button=Button(root, text='Click', command=dialog)
    button.grid(row=3, column=0, pady=5, padx=10)
    
    close=Button(root, text='Exit', command=root.quit)
    close.grid(row=4, column=0, pady=10)
    
    root.title('Multi radio button!')
    root.mainloop()

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      A "dispatch dictionary" may come in handy here:[CODE=python]
      >>> def funct1():
      ... print 'a'
      ...
      >>> def funct2():
      ... print 'b'
      ...
      >>> def funct2():
      ... print 'b'
      ...
      >>> def funct3():
      ... print 'c'
      ...
      >>> def funct4():
      ... print 'd'
      ...
      >>> dispatchDict = {2:funct1, 3:funct2, 6:funct3, 8:funct4}
      >>> actionCode = 3
      >>> dispatchDict[actionCode]()
      b
      >>> [/CODE]

      Comment

      Working...