Thread: tkinter event

    #1
  1. Caress me down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    Pennsylvania
    Posts
    289
    Rep Power
    511

    tkinter event


    alright im having a little trouble with tkinter events... i have the event set up so when you hit enter it starts a function. here is my code.

    Code:
    from Tkinter import *
    class App:
        def __init__(self, root):
            self.root = root
            root.bind("<Return>", self.place)
        def place(self):
            print "You hit enter"
    root = Tk()
    app  = App(root)
    root.mainloop()
    now it gives me this error when i hit enter.
    Code:
    Exception in Tkinter callback
    Traceback (most recent call last):
      File "C:\PYTHON2.4\lib\lib-tk\Tkinter.py", line 1345, in __call__
        return self.func(*args)
    TypeError: place() takes exactly 1 argument (2 given)
    i've tried adding "()" after self.place but then when i ran the program it ran self.place right away and nothing happened when i clicked enter.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Hi!

    When you hit enter, an event is passed to your function as a second argument. So you have to write
    Code:
    def place(self, event):
    Regards, mawe
  4. #3
  5. Caress me down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    Pennsylvania
    Posts
    289
    Rep Power
    511
    alright that works but now i have another problem... i also have a button that goes to self.place when you click it. when i click the button it gives an argument error. do i have to create 2 seperate functions, one for the button and one for the event?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    No, just write
    Code:
    def place(self, event=None)
    so it works with one and two arguments

IMN logo majestic logo threadwatch logo seochat tools logo