473,395 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Bind Escape to Exit

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
2 7363
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dowding George A | last post by:
Brent B. Welch gives this example in _Practical Programming in Tcl and Tk_. bindtags $t bind ViInsert <Escape> {bindtags %W {ViCmd %W all}} bind ViCmd <Key-i> {bindtags %W {ViInsert Text %W...
3
by: Peter Stojkovic | last post by:
I want EXIT a FOR-NEXT construct via EXIT if the user presses key ESCAPE How can I check, whether the user presses ESCAPE while my program is in the FOR-NEXT construct ?? Peter
6
by: Walter L. Preuninger II | last post by:
I need to convert escape sequences entered into my program to the actual code. For example, \r becomes 0x0d I have looked over the FAQ, and searched the web, with no results. Is there a...
3
by: Net Coder | last post by:
I'm trying to bind to DC by site. The environment is a AD 2K3 domain with multiple sites. For example, when my application is started, it checks for the site of the computer where the...
0
by: Asif Mohammed | last post by:
Hello, I have a datagridview bound to a database table with 2 columns. One is an ID column "NameID" which is hidden, the other is called "Name". The schema picture is here :...
0
by: Asif Mohammed | last post by:
Hello, I have a datagridview bound to a database table with 2 columns. One is an ID column "NameID" which is hidden, the other is called "Name". The schema picture is here :...
5
by: chandanlinster | last post by:
consider the following program /****************************************************/ #include <stdio.h> #include <stdlib.h> int main(void) { int c;
6
by: ARC | last post by:
I know this should be simple, but in a sub-routine that's printing a large batch of reports, how do you code the routine to look for an escape key to allow the user to break out? Thanks! Andy
4
by: Yoavo | last post by:
Hi, I want to close my form when the user presses the Escape key. I tried to catch the event KeyPress of the form but the program do not go through this code. I tried to catch the KeyPress event...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.