Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8496211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:47:21+00:00 2026-06-10T23:47:21+00:00

I am working on a rather simple wxpython GUI, and would like to be

  • 0

I am working on a rather simple wxpython GUI, and would like to be able to have the escape key close the window. Right now I just have a close button that executes sys.exit(0), but I’d like the escape key to do this to.

Does anyone know a way to do this?

import win32clipboard
import wx
from time import sleep
import sys

class MainFrame(wx.Frame):
    def __init__(self,title):

        wx.Frame.__init__(self, None, title="-RRESI Rounder-", pos=(0,0), size=(210,160))
        panel=Panel(self)

        icon = wx.Icon('ruler_ico.ico', wx.BITMAP_TYPE_ICO) 
        self.SetIcon(icon) 

class Panel(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        x1=10; x2=110
        y1=40; dy=25; ddy=-3
        boxlength=80

        self.button =wx.Button(self, label="GO", pos=(100,y1+dy*3))
        self.Bind(wx.EVT_BUTTON, self.OnClick,self.button)
        self.button.SetDefault()

        self.button2 =wx.Button(self, label="Close", pos=(100,y1+dy*4))
        self.Bind(wx.EVT_BUTTON, self.OnClose, self.button2)
        self.button.SetDefault()

        self.Bind(wx.EVT_KEY_UP, self.OnKeyUP)

        self.label1 = wx.StaticText(self, label="Input Number:", pos=(x1,y1+dy*1))
        self.Input = wx.TextCtrl(self, value="1.001", pos=(x2,ddy+y1+dy*1), size=(boxlength,-1))

        self.label0 = wx.StaticText(self, label="Round to closest:  1/", pos=(x1,y1+dy*0))
        self.Denominator = wx.TextCtrl(self, value="64", pos=(x2,ddy+y1+dy*0), size=(boxlength,-1))

        self.label2 = wx.StaticText(self, label="Output Number:", pos=(x1,y1+dy*2))
        self.display = wx.TextCtrl(self, value="1.0", pos=(x2,ddy+y1+dy*2), size=(boxlength,-1))
        self.display.SetBackgroundColour(wx.Colour(232, 232, 232))

        self.label3 = wx.StaticText(self, label="          ", pos=(x2+7,y1+dy*2+20)) #Copied

        self.label4 = wx.StaticText(self, label="Type value and hit Enter", pos=(x1-5,y1-dy*1.5))
        self.label5 = wx.StaticText(self, label="Output is copied", pos=(x1-5,y1-dy*1))
        font = wx.Font(8, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
        self.label4.SetFont(font)
        self.label5.SetFont(font)

    def OnKeyUP(self, event): 
        keyCode = event.GetKeyCode() 
        if keyCode == wx.WXK_ESCAPE: 
            sys.exit(0) 


    def OnClose(self, event):
        print "Closed"
        sys.exit(0)

    def OnClick(self,event):      
        print "You clicked the button!"

        def openClipboard():
            try:
                win32clipboard.OpenClipboard()
            except Exception, e:
                print e
                pass

        def closeClipboard():
            try:
                win32clipboard.CloseClipboard()
            except Exception, e:
                print e
                pass

        def clearClipboard():
            try:
                openClipboard()
                win32clipboard.EmptyClipboard()
                closeClipboard()
            except TypeError:
                pass

        def setText(txt): 
            openClipboard()
            win32clipboard.EmptyClipboard()
            win32clipboard.SetClipboardText(txt) 
            closeClipboard()

        Denominator = float(self.Denominator.GetValue())
        Input=float(self.Input.GetValue())
        Output=round(Input*Denominator,0)/Denominator
        self.display.SetValue(str(Output))
        setText(str(Output))
        self.label3.SetLabel("Copied")
        self.Update()#force redraw 
        sleep(.5)
        wx.Timer
        self.label3.SetLabel("                    ")


if __name__=="__main__":
    app = wx.App(redirect=False)   # Error messages don't go to popup window
    frame = MainFrame("RRESI Rounder")
    frame.Show()
    app.MainLoop()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:47:23+00:00Added an answer on June 10, 2026 at 11:47 pm

    This sorta works … albiet with some issues

    [edit]ok EVT_CHAR_HOOK works much better than EVT_KEY_UP

    import wx
    
    class Test(wx.Frame):
    
        def __init__(self):
            wx.Frame.__init__(self, None, -1, title='Event Test',
    size=(200, 200))
    
            panel = wx.Panel(self)
    
            panel.SetFocus()
    
            self.Bind(wx.EVT_CHAR_HOOK, self.OnKeyUP)
    
        def OnKeyUP(self, event):
            print "KEY UP!"
            keyCode = event.GetKeyCode()
            if keyCode == wx.WXK_ESCAPE:
                self.Close()
            event.Skip() 
    
    
    class App(wx.App):
        """Application class."""
    
        def OnInit(self):
            self.frame = Test()
            self.frame.Show()
            self.SetTopWindow(self.frame)
            self.frame.SetFocus()
            return True
    
    if __name__ == '__main__':
        app = App()
        app.MainLoop()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rather simple multi-threaded VCL gui application written with Delphi 2007. I
Working on a rather small, and simple layout, I decided to use Meyer's CSS
I've got a rather lengthy query i have been working with that is throwing
I'm an Ext veteran but have a few rather simple mobile apps i need
I'm working on a (rather) simple 2D project in OpenGL. It's some sort of
I'm rather newbie on Android, and I'm working on a simple application to get
I have created a rather simple photo gallery that I wish to apply PayPal
A rather simple question really. I'm working on a project where I need to
I search for a working solution for a rather simple problem, but could not
I am working on a simple GUI that outputs a numeric value that is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.