By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,199 Members | 1,120 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,199 IT Pros & Developers. It's quick & easy.

bind in Tkinter

P: n/a

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
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
VK
Shankar Iyer (si***@Princeton.EDU) wrote:
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?
Jul 19 '05 #2

P: n/a
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
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.