Help with Tkinter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fahadqureshi
    New Member
    • Oct 2007
    • 28

    Help with Tkinter

    i am trying to make a gui and need some help on how to link buttons to certain commands. here is my code:

    Code:
    from Tkinter import *
    root =Tk()
    root.geometry('300x200')
    root.title('EXAM')
    Label (text='Enter a Bus Number').pack(side=TOP,padx=10,pady=10)
    Entry(root, width=6).pack(side=TOP,padx=10,pady=10)
    Button(root, text='EXAM').pack(side= TOP, padx=10, pady=10)
    Button(root, text='Quit').pack(side= BOTTOM, padx=10, pady=10)
    root.mainloop()
    when i click on EXAM i want it to do grab the number thats enterd in the entry field so i can use it for further calculations. and when i click on quit i want it to exit the program. how do i go about doing this. and also i want to program to keep running unless i click on quit. for example i might want to enter more bus numbers. and the bus number will always be a 6 digit number so what would be advisable as far as definition. should i define it as an integer or a character or something.
  • Strider1066
    New Member
    • Aug 2007
    • 11

    #2
    Hi, being a newbie myself so take the following with a grain of salt.

    Most or many Tkinker objects can 'bind' to a method | function.
    Code:
     
    	def makeTButton(self, text):
    		self.a = TaskButton( self.frm, text = text)
    		self.a.configure(bg = 'green', width = 12)
    		[B]self.a.bind("<ButtonRelease-1>", self.tBtnClicked[/B]) 
    		self.a.pack()
    		return self.a._name
    tBtnClicked being a method

    There are several Tkinter references. See Tkinter reference: a GUI for Python from the New Mexico Tech Computer Center. Or see effbot.org

    Comment

    • ilikepython
      Recognized Expert Contributor
      • Feb 2007
      • 844

      #3
      Originally posted by fahadqureshi
      i am trying to make a gui and need some help on how to link buttons to certain commands. here is my code:

      Code:
      from Tkinter import *
      root =Tk()
      root.geometry('300x200')
      root.title('EXAM')
      Label (text='Enter a Bus Number').pack(side=TOP,padx=10,pady=10)
      Entry(root, width=6).pack(side=TOP,padx=10,pady=10)
      Button(root, text='EXAM').pack(side= TOP, padx=10, pady=10)
      Button(root, text='Quit').pack(side= BOTTOM, padx=10, pady=10)
      root.mainloop()
      when i click on EXAM i want it to do grab the number thats enterd in the entry field so i can use it for further calculations. and when i click on quit i want it to exit the program. how do i go about doing this. and also i want to program to keep running unless i click on quit. for example i might want to enter more bus numbers. and the bus number will always be a 6 digit number so what would be advisable as far as definition. should i define it as an integer or a character or something.
      You need to use the comman attribute of a button:
      [code=python]
      def onExam(ent):
      # do something with the entry

      from Tkinter import *
      root = Tk()
      root.geometry(' 300x200')
      root.title('EXA M')
      Label (root, text='Enter a Bus Number').pack(s ide=TOP,padx=10 ,pady=10)
      ent = Entry(root, width=6)
      ent.pack(side=T OP,padx=10,pady =10)
      Button(root, text='EXAM', command = lambda ent=ent: onExam(ent)).pa ck(side= TOP, padx=10, pady=10)
      Button(root, text='Quit', command = root.quit()).pa ck(side= BOTTOM, padx=10, pady=10)
      root.mainloop()
      [/code]
      I think it's better with a class though:
      [code=python]
      class MyGui(Frame):
      def __init__(self, parent=None):
      Frame.__init__( self, parent)

      Label(self, text='Enter a Bus Number').pack(s ide=TOP,padx=10 ,pady=10)
      self.ent = Entry(self, width=6)
      self.ent.pack(s ide=TOP,padx=10 ,pady=10)
      Button(self, text='EXAM', command = self.onExam).pa ck(side= TOP, padx=10, pady=10)
      Button(self, text='Quit', command = self.quit).pack (side= BOTTOM, padx=10, pady=10)

      def onExam(self):
      # do something with self.ent
      [/code]

      Comment

      • fahadqureshi
        New Member
        • Oct 2007
        • 28

        #4
        ok i went back and redid a lot of the code and decided to go a simpler route. now all i need the program to do is, upon the user pressing enter i want the program to take the 6 digit number in the entry field and use it for further calculations. the program runs ok as far as the gui showing up and all but when i press enter after entering the number in the entry field i get the folloing error.

        Exception in Tkinter callback
        Traceback (most recent call last):
        File "C:\Python23\Li b\lib-tk\Tkinter.py", line 1345, in __call__
        return self.func(*args )
        TypeError: Display() takes exactly 1 argument (2 given)

        and also for some reason the program wont quit when i press the Quit button.
        Here is the code
        Code:
        from Tkinter import *
        import tkMessageBox
        
        class GUIFramework(Frame):
        
            
            def __init__(self,master=None):
               
                Frame.__init__(self,master)
                
                """Window Title"""
                self.master.title("EXAM")
                self.master.geometry('250x350')
        
                self.grid(padx=10,pady=10)
                self.Exam()
               
            def Exam(self):
                                
                """Text"""
                self.lbText = Label(self, text="Enter Bus Number:")
                self.lbText.grid(row=0, column=1,padx=10,pady=10)
                
                """ Entry"""
                self.enText = Entry(self, width=6)
                self.enText.grid(row=1, column=1, padx=5, pady=5)
                self.enText.bind("<Return>", self.Display)  
                
                """Exit Button"""
                self.btnDisplay = Button(self, text="QUIT",font=( "Impat", "12", "bold"), command=self.exit())
                self.btnDisplay.grid(row=3, column=1, padx=50, pady=50)
         
                
            def Display(self):
                print self.enText.get()
        
            def exit(self):
                self.quit()
        
        
        if __name__ == "__main__":
            guiFrame = GUIFramework()
            guiFrame.mainloop()

        Comment

        • fahadqureshi
          New Member
          • Oct 2007
          • 28

          #5
          Disregard the previous post. i got the program to do what i wanted to do. now i am almost done with my project. the last and final problem i am having is that the gui wont close when i click on the quit button. I am sure there is a simple and quick fix to this but being a newbie i dont know what it is. when i click on quit the gui just freezes on the screen. here is the code

          Code:
          from Tkinter import *
          import tkMessageBox
          
          class GUIFramework(Frame):
          
              
              def __init__(self,master=None):
                  
                  """Initialise the base class"""
                  Frame.__init__(self,master)
                  
                  """Set the Window Title"""
                  self.master.title("EXAM & POUT")
                  self.master.geometry('200x300')
                  """Display the main window"""
                  self.grid(padx=10,pady=10)
                  self.lbText = Label(self, text="Enter Bus Number:")
                  self.lbText.grid(row=0, column=1,padx=5,pady=5)
                  
                  """Entry Box"""
                  self.enText = Entry(self, width=6)
                  self.enText.grid(row=1, column=1, padx=5, pady=5)
                          
                  """Button for Running EXAM"""
                  self.btnDisplay = Button(self, text="EXAM",font=( "Impat", "8", "bold"), command=self.exam)
                  self.btnDisplay.grid(row=2, column=1, padx=1, pady=1)
          
                  """Button for Running POUT"""
                  self.btnDisplay = Button(self, text="POUT",font=( "Impat", "8", "bold"), command=self.pout)
                  self.btnDisplay.grid(row=3, column=1, padx=1, pady=1)
          
                  """Button for Exiting the Program"""
                  self.btnDisplay = Button(self, text="EXIT",font=( "Impat", "12", "bold"), command=self.exit)
                  self.btnDisplay.grid(row=6, column=1, padx=20, pady=20)
                 
              def exam(self):
                  psspy.report_output(4,"",[0,0])
                  psspy.bsys(1,0,[0.0,0.0],0,[],1,[int(self.enText.get())],0,[],0,[])
                  psspy.exam(1,0)
          
                  
              def pout(self):
                  psspy.bsys(1,0,[0.0,0.0],0,[],1,[int(self.enText.get())],0,[],0,[])
                  psspy.pout(1,0)
          
              def exit(self):
                  global done # This global done i am not sure why it is here i copied some code i found online and this is what it had so i used it. if someone could clarify this it would be much appreciated
                  done = 1 # same thing with this 
                  self.destroy()
                  self.quit()
          
          
          
          if __name__ == "__main__":
              guiFrame = GUIFramework()
              guiFrame.mainloop()

          Comment

          • ilikepython
            Recognized Expert Contributor
            • Feb 2007
            • 844

            #6
            Originally posted by fahadqureshi
            Disregard the previous post. i got the program to do what i wanted to do. now i am almost done with my project. the last and final problem i am having is that the gui wont close when i click on the quit button. I am sure there is a simple and quick fix to this but being a newbie i dont know what it is. when i click on quit the gui just freezes on the screen. here is the code

            Code:
            from Tkinter import *
            import tkMessageBox
            
            class GUIFramework(Frame):
            
                
                def __init__(self,master=None):
                    
                    """Initialise the base class"""
                    Frame.__init__(self,master)
                    
                    """Set the Window Title"""
                    self.master.title("EXAM & POUT")
                    self.master.geometry('200x300')
                    """Display the main window"""
                    self.grid(padx=10,pady=10)
                    self.lbText = Label(self, text="Enter Bus Number:")
                    self.lbText.grid(row=0, column=1,padx=5,pady=5)
                    
                    """Entry Box"""
                    self.enText = Entry(self, width=6)
                    self.enText.grid(row=1, column=1, padx=5, pady=5)
                            
                    """Button for Running EXAM"""
                    self.btnDisplay = Button(self, text="EXAM",font=( "Impat", "8", "bold"), command=self.exam)
                    self.btnDisplay.grid(row=2, column=1, padx=1, pady=1)
            
                    """Button for Running POUT"""
                    self.btnDisplay = Button(self, text="POUT",font=( "Impat", "8", "bold"), command=self.pout)
                    self.btnDisplay.grid(row=3, column=1, padx=1, pady=1)
            
                    """Button for Exiting the Program"""
                    self.btnDisplay = Button(self, text="EXIT",font=( "Impat", "12", "bold"), command=self.exit)
                    self.btnDisplay.grid(row=6, column=1, padx=20, pady=20)
                   
                def exam(self):
                    psspy.report_output(4,"",[0,0])
                    psspy.bsys(1,0,[0.0,0.0],0,[],1,[int(self.enText.get())],0,[],0,[])
                    psspy.exam(1,0)
            
                    
                def pout(self):
                    psspy.bsys(1,0,[0.0,0.0],0,[],1,[int(self.enText.get())],0,[],0,[])
                    psspy.pout(1,0)
            
                def exit(self):
                    global done # This global done i am not sure why it is here i copied some code i found online and this is what it had so i used it. if someone could clarify this it would be much appreciated
                    done = 1 # same thing with this 
                    self.destroy()
                    self.quit()
            
            
            
            if __name__ == "__main__":
                guiFrame = GUIFramework()
                guiFrame.mainloop()
            Try just calling the self.quit() or maybe try self.master.qui t(). I don't know about the global done part. Probably the writer of the program set this varaible to 1 when it exited but I don't think you need that.

            Comment

            • fahadqureshi
              New Member
              • Oct 2007
              • 28

              #7
              I have tried self.quit() and self.master.qui t() and it just freezes on the screen. What's odd is that it closes when i click on the x in the windows menu bar. Oh well, thanks for all the help i have decided to just not inculde the quit button and just tell the users to use the x to close it.
              Thanks again.

              Comment

              Working...