How to link radio select with dialog in Tkinter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joseff
    New Member
    • Apr 2007
    • 2

    How to link radio select with dialog in Tkinter

    Hi evryone!!
    I' m very new not only to Python and Tkinter but to programing.
    I'm a bit stuck here and would be grateful for some help.
    I have 3 radio select buttons(see below) which I want to link to a pmw dialog with (apply and cancel options ) i.e. when I press enable btn dialog pops up and asks to confirm the my choice and if apply is selected then run my enable script if cancel then close the dialog. the same with the other radio buttons,if disable is selected then the same dialog appears and ask again to apply or cancel.
    So far I got the buttons working and I get the different values printed on the console and if I uncoment the second ""choice=v.bloc k..."" this runs the script but at the moment from the "test" button. I don't need the buton and the console output instead I just want to be able to responce to the buttons via the dialog. I not sure if this makes any sence . this is some of my code so far.
    thanks
    Code:
    def myDialog(w):
        dialog = Pmw.Dialog(root, buttons=('Apply', 'Cancel'),defaultbutton='Apply',title='ConfirmSelection',command=block_firewall())
        return  w
    
    def block_firewall():
    	pipe_block = os.popen("./block-iptables", 'r')
    
    def call():
        choice = v.get()
        #choice= v.block_firewall()
        print '"Input %s "' % variant
        #if choice
    
    global v
    v = IntVar()
    
    radiobutton = Radiobutton(frame,text= "Enable", variable=v, value="1")
    radiobutton.pack(side=TOP,anchor=W)
    
    radiobutton = Radiobutton(frame,text= "Disable", variable=v, value="2")
    radiobutton.pack(side=TOP,anchor=W)
    
    radiobutton = Radiobutton(frame,text= "Block All", variable=v, value="3")
    radiobutton.pack(side=TOP,anchor=W)
    
    radiobutton.select()
    
    test = Button(frame,text="Select",command=call)
    test.pack()
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by joseff
    Hi evryone!!
    I' m very new not only to Python and Tkinter but to programing.
    I'm a bit stuck here and would be grateful for some help.
    I have 3 radio select buttons(see below) which I want to link to a pmw dialog with (apply and cancel options ) i.e. when I press enable btn dialog pops up and asks to confirm the my choice and if apply is selected then run my enable script if cancel then close the dialog. the same with the other radio buttons,if disable is selected then the same dialog appears and ask again to apply or cancel.
    So far I got the buttons working and I get the different values printed on the console and if I uncoment the second ""choice=v.bloc k..."" this runs the script but at the moment from the "test" button. I don't need the buton and the console output instead I just want to be able to responce to the buttons via the dialog. I not sure if this makes any sence . this is some of my code so far.
    thanks
    Code:
    def myDialog(w):
        dialog = Pmw.Dialog(root, buttons=('Apply', 'Cancel'),defaultbutton='Apply',title='ConfirmSelection',command=block_firewall())
        return  w
    
    def block_firewall():
    	pipe_block = os.popen("./block-iptables", 'r')
    
    def call():
        choice = v.get()
        #choice= v.block_firewall()
        print '"Input %s "' % variant
        #if choice
    
    global v
    v = IntVar()
    
    radiobutton = Radiobutton(frame,text= "Enable", variable=v, value="1")
    radiobutton.pack(side=TOP,anchor=W)
    
    radiobutton = Radiobutton(frame,text= "Disable", variable=v, value="2")
    radiobutton.pack(side=TOP,anchor=W)
    
    radiobutton = Radiobutton(frame,text= "Block All", variable=v, value="3")
    radiobutton.pack(side=TOP,anchor=W)
    
    radiobutton.select()
    
    test = Button(frame,text="Select",command=call)
    test.pack()
    I was going to say that you should have a look at this thread and post back, but looking at your code, we'll want to work on your structure a bit.
    In the mean time, have a look around, read some of the Posting Guidelines so that I won't have to add CODE tags for you.

    Thanks for joining the Python Forum on TheScripts.com.

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by bartonc
      I was going to say that you should have a look at this thread and post back, but looking at your code, we'll want to work on your structure a bit.
      In the mean time, have a look around, read some of the Posting Guidelines so that I won't have to add CODE tags for you.

      Thanks for joining the Python Forum on TheScripts.com.
      Code:
      from Tkinter import *
      
      ## The trick is to SUB CLASS the widget that you want to add fuctionality to ##
      ## I don't have PMW, but the idea is the same: a Tkinter.Frame is just a blank ##
      ## frame until you subclass it an give it something to do ##  HAVE FUN
      
      class MyFrame(Frame):
          """A subclass of Tkinter.Frame that has some radio buttons that share a StringVar."""
          def __init__(self, root, *args, **kwargs):
              Frame.__init__(self, root, *args, **kwargs)
      
              # Create a variable to link the radio buttons
              self.RadioButtonLink = RadioButtonLink = StringVar()
              RadioButtonLink.set("On")  # or whatever you want the default to be
      
              # Create the radio buttons and arrange them on the frame
              onRadio = Radiobutton(self, text="On", value="On", variable=RadioButtonLink)
              onRadio.grid(row=0, column=0)
              offRadio = Radiobutton(self, text="Off", value="Off", variable=RadioButtonLink)
              offRadio.grid(row=0, column=1)
              noneRadio = Radiobutton(self, text="None", value="None", variable=RadioButtonLink)
              noneRadio.grid(row=0, column=2)
      
              # This is just so we have a way to make an event #
              selectButton = Button(self, text='Select')
              selectButton.grid(row=1, column=0, columnspan=3)
              selectButton.bind("<Button-1>", self.OnButton)
      
          def OnButton(self, event):
              """Print the value of the selected radio button."""
              print self.RadioButtonLink.get()
      
      
      if __name__ == "__main__":
          root = Tk()
      
          frame = MyFrame(root)
          frame.pack()
      
      
          root.mainloop()

      Comment

      • joseff
        New Member
        • Apr 2007
        • 2

        #4
        many thanks for the help guys , I got it working exactly as I wanted now tanks to you!!!
        I hope one day I would be able to help others.

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by joseff
          many thanks for the help guys , I got it working exactly as I wanted now tanks to you!!!
          I hope one day I would be able to help others.
          You are welcome. I LIKE your attitude.

          Keep posting,
          Barton

          Comment

          Working...