bind in Tkinter

  • Thread starter Shankar Iyer (siyer
  • Start date
S

Shankar Iyer (siyer

I have been trying to learn how to associate keyboard events with actions taken by a Python program using Tkinter. From what I've read online, it seems that this is done with the bind methods. In one of my programs, I have included the following:

self.enternumber = Entry(self)
self.enternumber.bind("<Return>",self.quit)
self.enternumber.pack({"side":"top"})

It seems to me that, as a result of this code, if the enternumber Entry widget is selected and then the <Return> key is pressed, then the program should quit. Indeed, it seems that the program does attempt to quit, but instead an error message appears claiming that quit() takes 1 argument but 2 are given. I get the same type of error if I replace self.quit with some function that I have written. I am not sure how to fix this problem and hope that someone here can spot my error. Thanks for your help.

Shankar
 
V

VK

Shankar said:
I have been trying to learn how to associate keyboard events with actions taken by a Python program using Tkinter. From what I've read online, it seems that this is done with the bind methods. In one of my programs, I have included the following:

self.enternumber = Entry(self)
self.enternumber.bind("<Return>",self.quit)
self.enternumber.pack({"side":"top"})

It seems to me that, as a result of this code, if the enternumber Entry widget is selected and then the <Return> key is pressed, then the program should quit. Indeed, it seems that the program does attempt to quit, but instead an error message appears claiming that quit() takes 1 argument but 2 are given. I get the same type of error if I replace self.quit with some function that I have written. I am not sure how to fix this problem and hope that someone here can spot my error. Thanks for your help.

Shankar
Have you defined quit function?
 
M

Markus Weihs

Hi!

If you press a key, a key-event is passed to the function, here to
self.quit. This is the misterious second argument, which can be
useful if you e.g. want to check which key was pressed. Here is
a snippet to show how you can do it:


from Tkinter import *

def quit_program(event):
print event.keysym # which key was pressed?
root.quit()

root = Tk()
e = Entry()
e.bind("<Return>", quit_program)
e.pack()
root.mainloop()


Regards, mawe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top