#1
  1. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407

    Tkinter button events


    Im tring to to create this little practice program that ... when you click on OK button another button shows up but it does not seem to be working or i dont know how to do it right.. any ideas?

    Code:
    from Tkinter import *
    
    class GUI:
    
          def __init__ (self, master):
    
              frame = Frame(master)
              frame.pack()
    
              self.label = Label(frame,{"text":"This is just a test program"})
              self.label.pack()
    
              self.button = Button(frame)
              self.button.configure(text="OK",background="blue",foreground="white",command=self.buttonClick(frame));
              self.button.pack(side=LEFT)
    
    
          def buttonClick(self,master):
    
              self.button1 = Button(master,text="Click")
              self.button1.pack(side=LEFT)
    
    
    
    
    if __name__ == "__main__":
    
       root = Tk()
       root_gui = GUI(root)
       root.mainloop()
  2. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    You were close....
    You have to give your Tkinter button a reference to the function that is to be called - so just it's name not the complete thing

    In which case you need to track the main root window so you can hang buttons on it. The easiest way is to use an instance variable like self.master. This restriction is a major reason why most Tkinter programs are base around classes.

    Code:
    from Tkinter import*
    
    class GUI: 
    
          def __init__(self, master): 
    
              self.master = master
              frame = Frame(self.master)
              frame.pack()
    
              self.label = Label(frame, {"text": "This is just a test program"})
              self.label.pack()
    
              self.button = Button(frame)
              self.button.configure(text = "OK", background = "blue", 
                    foreground = "white", command = self.buttonClick); 
              self.button.pack(side = LEFT)
    
    
          def buttonClick(self): 
    
              self.button1 = Button(self.master, text = "Click")
              self.button1.pack(side = LEFT)
    
    
    
    
    if __name__ == "__main__": 
       root = Tk()
       root_gui = GUI(root)
       root.mainloop()
    Grim
  4. #3
  5. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    BTW out of interest, what text editor are you using? Your tabs are 3 chars and not 4. Not a problem if you only write your own stuff but it might get messy if you try pick-a-mix coding
  6. #4
  7. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    hehe nice.. good thing i was close for now on i will just something like.. what you did

    Code:
    self.master = master
    im using context on windows.. do you have anything better? as im looking fro a better text editor.. btw how did you add them colors to your code tags
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    84
    Rep Power
    11
    you could also use a lambda function, if you want to avoid creating too many instance variables (though the root window is pretty much always one you will want to give your entire class access to).

    Code:
    Button(self, text="Test", command=lambda: self.somefunction(somevariable))
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    103
    Rep Power
    11
    Code:
    print 'I think he used his py2html'
    print 'You can find it :'
    Here
    It is not important if the glass is half full or half empty.What is important,is who has been drinking from MY glass?!?!?
  12. #7
  13. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    You don't have to like my colours but if you use IDLE you might recognise them. PHP's built in colouriser is . I wanted something like it but that could generate more or less any markup. The GUI layout is a bit Mickey Mouse but I'm working on it, in fact I'm planning a complete re-work of most of the code

    I prefer TextPad as an editor but I do use IDLE a lot.

    Grim

IMN logo majestic logo threadwatch logo seochat tools logo