473,811 Members | 3,256 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 7429
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
1541
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 my creation. Tkinter provides widget.bind, widget.bind_class(), and widget.bind_all(), but from...
3
1273
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
6658
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 want to see the output::
3
1836
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 that DC on the site. If the DC is not available, then a default server, the PDC emulator is used.
0
9101
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 editing/adding etc in the grid (see code event)
0
1309
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 editing/adding etc in the grid (see code event)
5
2544
by: chandanlinster | last post by:
consider the following program /****************************************************/ #include <stdio.h> #include <stdlib.h> int main(void) { int c;
6
3396
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
16420
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 right way to close the form when the user presses Escape ?
0
9731
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10651
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10405
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10136
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9208
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7671
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4342
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
2
3871
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.