Bind Escape to Exit

B

Binny V A

Hello Everyone,

I am new to python and I am trying to get a program
to close a application when the Escape Key is pressed.

This is the code that I used

---------------------------------
from Tkinter import *

class Application(Frame):
def createWidgets(self):
self.lab = Label(text="Hello World")
self.lab.pack()

def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
self.bind('<Key-Escape>',self.quit)

app = Application()
app.mainloop()

---------------------------------

It is displaying everything properly, but it is not quiting
when the escape key is pressed.

What am I doing wrong

Thank You,
Binny V A
http://www.geocities.com/binnyva/code
 
H

Harlin Seritt

Binny,

The only way I could think to get this done was like so:

from Tkinter import *

class Application: # take away the inherited class

def end(self, event):
self.master.destroy()

def createWidgets(self):
self.lab = Label(text="Hello World")
self.lab.pack()

def __init__(self, master):
self.master = master # Create a class version of master:
self.master
self.frame = Frame(self.master) # Change master to self.master
self.frame.pack()
self.createWidgets()
self.master.bind('<Escape>', self.end) # Change <Key-Escape> to
<Escape>, link bind to self.end

root = Tk() # Use a tk instance
a = Application(root)
root.mainloop() # Call tk.mainloop()

I am almost certain that I had the same dilemma as you. Good luck. If
you find a better way please post it.

Thanks,

Harlin Seritt
 
K

Kent Johnson

Binny said:
Hello Everyone,

I am new to python and I am trying to get a program
to close a application when the Escape Key is pressed.

Here is a version that works. The changes from yours:
- Bind <Escape>, not <Key-Escape>
- Bind the key to the root, not the frame
- Define a quit() method that takes an event parameter

from Tkinter import *

class Application(Frame):
def createWidgets(self):
self.lab = Label(text="Hello World")
self.lab.pack()

def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
master.bind('<Escape>',self.quit)

def quit(self, event):
Frame.quit(self)

root=Tk()
app = Application(root)
app.mainloop()


Kent
 
M

Martin Franklin

Kent said:
Here is a version that works. The changes from yours:
- Bind <Escape>, not <Key-Escape>

These amount to the same thing AFAIK
- Bind the key to the root, not the frame

This is the 'fix' Tkinter's Tk (and Toplevel) widgets will allow key
bindings, Frames do not.
- Define a quit() method that takes an event parameter

Yep the bind'ing will pass an event object to the callback.

just FYI, you may be interested in the Tkinter mailing list:-

http://mail.python.org/mailman/listinfo/tkinter-discuss

also available from gmane:-

gmane.comp.python.tkinter


Martin.
 

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

Similar Threads


Members online

Forum statistics

Threads
474,222
Messages
2,571,142
Members
47,756
Latest member
JulienneY0

Latest Threads

Top