473,574 Members | 2,291 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Fra me):
def createWidgets(s elf):
self.lab = Label(text="Hel lo World")
self.lab.pack()

def __init__(self, master=None):
Frame.__init__( self, master)
self.pack()
self.createWidg ets()
self.bind('<Key-Escape>',self.q uit)

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 7393
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.des troy()

def createWidgets(s elf):
self.lab = Label(text="Hel lo World")
self.lab.pack()

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

root = Tk() # Use a tk instance
a = Application(roo t)
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(Fra me):
def createWidgets(s elf):
self.lab = Label(text="Hel lo World")
self.lab.pack()

def __init__(self, master=None):
Frame.__init__( self, master)
self.pack()
self.createWidg ets()
master.bind('<E scape>',self.qu it)

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

root=Tk()
app = Application(roo t)
app.mainloop()
Kent

This is the code that I used

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

class Application(Fra me):
def createWidgets(s elf):
self.lab = Label(text="Hel lo World")
self.lab.pack()

def __init__(self, master=None):
Frame.__init__( self, master)
self.pack()
self.createWidg ets()
self.bind('<Key-Escape>',self.q uit)

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
1534
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 all}} I want to do something along those lines. Tkinter provides a bindtags method for each widget, but I can't figure out how to bind to a tag of...
3
1265
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
6634
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 function that can do this, or do I need to use predefined constants or a table of the values? Below is a sample program. When run with the input w\rx, I...
3
1812
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 application is running. Then, it looks for a DC in the site and then all DirectorySearcher and DIrectoryEntry operations will use a binding string aimed at...
0
9089
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 : http://img89.imageshack.us/img89/1251/dbschemaep9.jpg What happens is : 1. Form loads with values from database inside the DGV 2. I press the checkbox to enable...
0
1290
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 : http://img89.imageshack.us/img89/1251/dbschemaep9.jpg What happens is : 1. Form loads with values from database inside the DGV 2. I press the checkbox to enable...
5
2534
by: chandanlinster | last post by:
consider the following program /****************************************************/ #include <stdio.h> #include <stdlib.h> int main(void) { int c;
6
3382
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
16410
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 for several conrtrols in the dialog and the event was caught, but I do not want to catch this event for every control in the form. What is the...
0
7801
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7808
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8096
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6450
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5299
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3749
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1055
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.