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

Bind Escape to Exit

P: n/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
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

Jul 18 '05 #2

P: n/a
Binny V A wrote:
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

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.

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.