Combined event handling on Tkinkter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nk28
    New Member
    • Jan 2010
    • 26

    Combined event handling on Tkinkter

    Hey

    I am making a GUI Program in Tkinter and am running into problems.What I want to do is draw 2 checkboxes and a button. According to the user input next steps should take place. A part of my code has been shown below :-
    Code:
    CheckVar1 = IntVar()
    CheckVar2 = IntVar()
    self.C1 = Checkbutton(root, text = "C Classifier", variable = CheckVar1, onvalue = 1, offvalue = 0, height=5,width = 20).grid(row=4)
    
    self.C2 = Checkbutton(root, text = "GClassifier", variable = CheckVar2, onvalue = 1,    offvalue = 0, height=5, width = 20).grid(row=5)
    
    self.proceed1 = Button(root,text = "\n Proceed",command =       self.proceed(CheckVar1.get(),CheckVar2.get())).grid(row=6)
    
    # where proceed prints the combined values of 2 checkboxes
    The error that I am getting is typical ie a default value of both the selected checkboxes gets printed up and then there is no further input. The error that I get is NullType Object is not callable.

    I searched on the net and I think the answer is related to lambda events or curry.

    Please help ..
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    You just call the function without the args. You can access self.* variable anywhere in the program so passing them is not necessary, but I can't tell much else from this snippet. Note also the difference between the output of these two lines (why are they different?).
    Code:
    self.proceed1 = Button(root,text = "\n Proceed",command =self.proceed).grid(row=6)
    #
    self.proceed2 = Button(root,text = "\n Proceed",command =self.proceed)
    self.proceed2.grid(row=6)
    #
    print self.proceed1
    print self.proceed2

    Comment

    • dwblas
      Recognized Expert Contributor
      • May 2008
      • 626

      #3
      Sorry, I left out that you want to make the variables members of the class so they can be accessed anywhere.
      Code:
      self.CheckVar1 = IntVar()
      self.CheckVar2 = IntVar()

      Comment

      • nk28
        New Member
        • Jan 2010
        • 26

        #4
        Code:
        class NextScreen():
                
                def __init__(self,root,val):
                        self.CheckVar1 = IntVar()
                        self.CheckVar2 = IntVar()
                        self.label = Label(root,text = " \n Click on what do you wish to do \n ").grid(row = 0)
                        self.calculate = Button(root,text = "CALCULATE THE THRESHOLDS").grid(row = 1,column = 0)
                        self.printed = Button(root,text = "PRINT THE RESULTS").grid(row = 1,column = 1)
                        self.space = Label(root , text = "\n Also select the classifier").grid(row = 3)
                        self.C1 = Checkbutton(root, text = "C Classifier", variable = CheckVar1, onvalue = 1, offvalue = 0, height=5,width = 20).grid(row=4)
                        self.C2 = Checkbutton(root, text = "GClassifier", variable = CheckVar2, onvalue = 1, offvalue = 0, height=5, width = 20).grid(row=5)
                        self.proceed1 = Button(root,text = "\n Proceed",command = self.proceed()).grid(row=6)
                        self.menu = self.makeMenuBar(root)
                def proceed(self):
                        val1 = self.CheckVar1.get()
                        val2 = self.CheckVar2.get()
        Here I get the error that CheckVar1 needs to be global.
        I am nt understanding what is wrong in here.....

        Comment

        • dwblas
          Recognized Expert Contributor
          • May 2008
          • 626

          #5
          No way to tell without the complete error message and line number. A quick check shows that you did not change this line or the following line
          self.C1 = Checkbutton(roo t, text = "C Classifier", variable = CheckVar1, onvalue = 1, offvalue = 0, height=5,width = 20).grid(row=4)
          also it should be
          Button(root,tex t = "\n Proceed",comman d = self.proceed) <-- no parens
          to call the function when the mouse is released.

          Comment

          • nk28
            New Member
            • Jan 2010
            • 26

            #6
            Checkbox Values not coming correct

            Hey , I got my mistake which was that in checkbox command the variable should be self.CheckVar1. But I have ran into problems. I wanted 2 checkboxes and a button which would print individual boxes values. The code is shown below but I can't figure out what I am doing wrong.

            Code:
             def __init__(self,root):
                            self.CheckVar1 = IntVar()
                            self.CheckVar2 = IntVar()
                            self.C1 = Checkbutton(root, text = "C Classifier", variable = self.CheckVar1, height=5,width = 20)
                            self.C1.grid(row=4)
                            self.C2 = Checkbutton(root, text = "GClassifier", variable = self.CheckVar2, height=5, width = 20)
                            self.C2.grid(row=5)
                            self.proceed1 = Button(root,text = "\n Proceed",command = self.proceed)
                            self.proceed1.grid(row=6)
            def proceed(self):
                            val1 = self.CheckVar1.get()
                            val2 = self.CheckVar2.get()
                            print val1,val2
            I thought that I would get mixtures of 0 and 1 corresponding to whether they are selected or not . But all I am getting is 0 and 0.

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              It seems to work properly. In the following test script, Checkbutton 1 is initially set to on. When the Button is clicked, the output is "1 0" as expected.
              Code:
              from Tkinter import *
              
              class Test:
                  def __init__(self,root):
                      self.CheckVar1 = IntVar()
                      self.CheckVar2 = IntVar()
                      self.C1 = Checkbutton(root, text="C Classifier", variable=self.CheckVar1, height=5,width=20)
                      self.C1.grid(row=4)
                      self.C2 = Checkbutton(root, text="G Classifier", variable=self.CheckVar2, height=5, width=20)
                      self.C2.grid(row=5)
                      self.proceed1 = Button(root,text="\n Proceed",command=self.proceed)
                      self.proceed1.grid(row=6)
                      self.C1.invoke()
                  def proceed(self):
                      val1 = self.CheckVar1.get()
                      val2 = self.CheckVar2.get()
                      print val1,val2
              
              if __name__ == "__main__":
                  root = Tk()
                  app = Test(root)
                  root.mainloop()

              Comment

              • nk28
                New Member
                • Jan 2010
                • 26

                #8
                Hey I am not understanding what is wrong with my code.
                What I do I am telling you in comments and not code.

                I copy the above Test class as it is
                Code:
                class Test:
                    ....
                    ....
                class test:
                    def __init__(self,root):
                           ....
                    def change(self):
                           if(a==1): # any checking condition
                               root.destroy()
                               t1 = Tk()
                               App = Test(t1)
                               t1.mainloop()
                start = Tk()
                New = test(start)
                start.mainloop()
                The above code for Test class written by bvdet works fine but when I add this into my class as shown the checkboxes only show 0. I don't understand why??

                Comment

                • bvdet
                  Recognized Expert Specialist
                  • Oct 2006
                  • 2851

                  #9
                  It would help us help you if you would post complete code, or at least enough code to run without external dependencies, so we can test it without having to complete the code for testing ourselves.

                  Comment

                  • nk28
                    New Member
                    • Jan 2010
                    • 26

                    #10
                    Complete program : not working the way I want to

                    Code:
                    from Tkinter import *
                    
                    class NextScreen():
                            def __init__(self,root):
                                    self.Check1 = IntVar()
                                    self.Check2 = IntVar()
                                    self.space = Label(root , text = "\n Also select the classifier").grid(row = 0)
                                    self.C1 = Checkbutton(root, text = "C", variable = self.Check1,offvalue=5,onvalue = 1, height=5,width = 20)
                                    self.C1.grid(row=1)
                                    self.C2 = Checkbutton(root, text = "G", variable = self.Check2,offvalue=0,onvalue =1, height=5, width = 20)
                                    self.C2.grid(row=2)
                                    self.C1.invoke()
                                    self.button = Button(root,text = "Proceed",command = self.proceed)
                                    self.button.grid(row=3)
                                    #print self.Check1.get(),self.Check2.get()
                                    
                            def proceed(self):
                                    print self.Check1.get(),self.Check2.get()
                                    
                           
                    
                    
                    class Screen():
                            
                            def __init__(self,root):
                                    var = IntVar()
                                    self.label = Label(root,text = " \n Welcome \n ").pack()
                                    self.radio1 = Radiobutton(root, value=1,indicatoron=False,text="Face",variable=var,command= lambda : self.select(root,var.get())).pack()
                                    
                                    self.radio3 = Radiobutton(root, value=2,indicatoron=False,text="FFinger",variable=var,command=lambda : self.select(root,var.get())).pack()
                    
                            def select(self,root,value):
                                    root.withdraw()
                                    if(value==1):
                                            
                                            Face = Tk()
                                            Face.title("Face and Face NIST Database ")
                                            App = NextScreen(Face)
                                            Face.geometry("400x400")
                                            Face.resizable(1,1)
                                            Face.mainloop()
                                    else:
                                            if(value==2):
                                                    
                                                    Face_Fin = Tk()
                                                    Face_Fin.title("Face and Finger NIST Database ")
                                                    App = NextScreen(Face_Fin)
                                                    Face_Fin.geometry("400x400")
                                                    Face_Fin.resizable(1,1)
                                                    Face_Fin.mainloop()
                                    
                    if __name__ == "__main__":
                            root = Tk()
                            root.title('Fusion in Multibiometrics')
                            App = Screen(root)
                            root.geometry("260x220")
                            root.resizable(1,1)
                            root.mainloop()
                    Here I want that different combination of 1 and 0 should be printed for different selection of checkboxes

                    Comment

                    • dwblas
                      Recognized Expert Contributor
                      • May 2008
                      • 626

                      #11
                      There are two possible problems. You'll have to test for yourself to see if either or both are the problem. One is using both pack() and grid(), which every tutorial AFAIK says you can't do. The second is multiple instances of Tk, which no one does so the outcome is not known, since no one does it. Part of the offending code follows, modified so as to use better techniques.
                      Code:
                      from Tkinter import *
                       
                      class NextScreen():
                              def __init__(self,root2, title_in):
                                      root2.title(title_in)
                                      root.geometry("250x300")
                                      self.Check1 = IntVar()
                                      self.Check2 = IntVar()
                                      self.screen_frame = Frame(root)
                                      self.screen_frame.pack()
                                      self.space = Label(self.screen_frame , text = "\n Also select the classifier").pack()
                                      self.C1 = Checkbutton(self.screen_frame, text = "C", variable = self.Check1, height=5,width = 20)
                                      self.C1.pack()
                                      self.C2 = Checkbutton(self.screen_frame, text = "G", variable = self.Check2, height=5, width = 20)
                                      self.C2.pack()
                                      self.C1.invoke()
                                      self.button = Button(self.screen_frame,text = "Proceed",command = self.proceed)
                                      self.button.pack()
                                      self.exit = Button(self.screen_frame, text="Exit", command=root.quit)
                                      self.exit.pack()
                      
                              def proceed(self):
                                      print self.Check1.get(),self.Check2.get()
                       
                       
                      class Screen():
                       
                          def __init__(self,root):
                              self.root = root
                              self.screen_frame = Frame(self.root)
                              self.screen_frame.pack()
                              var = IntVar()
                              self.label = Label(self.screen_frame,text = " \n Welcome \n ")
                              self.radio1 = Radiobutton(self.screen_frame, value=1,indicatoron=False,text="Face",variable=var,command= lambda : self.select(var.get()))
                              self.radio3 = Radiobutton(self.screen_frame, value=2,indicatoron=False,text="FFinger",variable=var,command=lambda : self.select(var.get()))
                              self.label.pack()
                              self.radio1.pack()
                              self.radio3.pack()
                       
                          def select(self,value):
                              self.screen_frame.destroy()
                              if(value==1):
                                  App = NextScreen(self.root, "Face and Face NIST Database ")
                      
                      if __name__ == "__main__":
                              root = Tk()
                              root.title('Fusion in Multibiometrics')
                              App = Screen(root)
                              root.resizable(1,1)
                              root.mainloop()

                      Comment

                      • nk28
                        New Member
                        • Jan 2010
                        • 26

                        #12
                        Hey ,

                        Thanks for replying ....

                        I understood what you are saying but unfortunately the code that you modified isn't working. The value in select always gets 0 . I don't know that also why is it happening...

                        Thanks a lot for answering though...

                        Comment

                        • dwblas
                          Recognized Expert Contributor
                          • May 2008
                          • 626

                          #13
                          You have to click the "Proceed" button. The values will then print in the terminal window. Zero for off and one for on, depending on whether the checkbox is checked. Although I have no idea if you are running from a terminal or IDE, or which version of Python you are using, but it works fine on my Slackware system running in xterm, and getting it to work properly on my computer is all that I can do. You had this same problem in the previous post, so the problem is possibly your Python installation or OS which we obviously can not help with.

                          Comment

                          • nk28
                            New Member
                            • Jan 2010
                            • 26

                            #14
                            I am sorry but I can't get it how you are getting the output.

                            The value in select is always 0 and never turns 1 .
                            Also when I write (value == 0 ) in place of 1 and click on proceed subsequently the only output is 0, 0

                            Comment

                            • bvdet
                              Recognized Expert Specialist
                              • Oct 2006
                              • 2851

                              #15
                              The value in select() can only be 1 or 2 because the method is called by pushing one of the buttons. IntVar object var is initially set to 0, but becomes 1 or 2 on a button invocation.

                              Comment

                              Working...