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 871209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:36:21+00:00 2026-05-15T10:36:21+00:00

Could someone show me how I could return a value from a wxPython Frame?

  • 0

Could someone show me how I could return a value from a wxPython Frame? When the use clicks close, I popup a message dialog asking him a question. I would like to return the return code of this message dialog to my calling function.

Thanks

  • 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-05-15T10:36:22+00:00Added an answer on May 15, 2026 at 10:36 am

    Because the wxFrame has events that process via the app.MainLoop() functionality, the only way to get at the return value of a wx.Frame() is via catching an event.

    The standard practice of handling events is typically from within the class which derives from wx.Window itself (e.g., Frame, Panel, etc.). Since you want code exterior to the wx.Frame to receive information that was gathered upon processing the OnClose() event, then the best way to do that is to register an event handler for your frame.

    The documentation for wx.Window::PushEventHandler is probably the best resource and even the wxpython wiki has a great article on how to do this. Within the article, they register a custom handler which is an instance of "MouseDownTracker." Rather than instantiating within the PushEventHandler call, you’d want to instantiate it prior to the call so that you can retain a handle to the EventHandler derived class. That way, you can check on your derived EventHandler class-variables after the Frame has been destroyed, or even allow that derived class to do special things for you.

    Here is an adaptation of that code from the wx python wiki (admittedly a little convoluted due to the requirement of handling the results of a custom event with a "calling" function):

    import sys
    import wx
    import wx.lib.newevent
        
    (MyCustomEvent, EVT_CUSTOM) = wx.lib.newevent.NewEvent()
        
    class CustomEventTracker(wx.EvtHandler):
        def __init__(self, log, processingCodeFunctionHandle):
            wx.EvtHandler.__init__(self)
            self.processingCodeFunctionHandle = processingCodeFunctionHandle
            self.log = log
            EVT_CUSTOM(self, self.MyCustomEventHandler)
        
        def MyCustomEventHandler(self, evt):
            self.log.write(evt.resultOfDialog + '\n')
            self.processingCodeFunctionHandle(evt.resultOfDialog)
            evt.Skip()
        
    class MyPanel2(wx.Panel):
        def __init__(self, parent, log):
            wx.Panel.__init__(self, parent)
            self.log = log
        
        def OnResults(self, resultData):
            self.log.write("Result data gathered: %s" % resultData)
        
    class MyFrame(wx.Frame):
        def __init__(self, parent, ID=-1, title="", pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE):
            wx.Frame.__init__(self, parent, ID, title, pos, size, style)
            self.panel = panel = wx.Panel(self, -1, style=wx.TAB_TRAVERSAL | wx.CLIP_CHILDREN | wx.FULL_REPAINT_ON_RESIZE)
            sizer = wx.BoxSizer(wx.VERTICAL)
            sizer.Add((25, 25))
        
            row = wx.BoxSizer(wx.HORIZONTAL)
            row.Add((25,1))
            m_close = wx.Button(self.panel, wx.ID_CLOSE, "Close")
            m_close.Bind(wx.EVT_BUTTON, self.OnClose)
            row.Add(m_close, 0, wx.ALL, 10)
            sizer.Add(row)
            self.panel.SetSizer(sizer)
        
        def OnClose(self, evt):
            dlg = wx.MessageDialog(self, "Do you really want to close this frame?", "Confirm Exit", wx.OK | wx.CANCEL | wx.ICON_QUESTION)
            result = dlg.ShowModal()
            dlg.Destroy()
            if result == wx.ID_CANCEL:
                event = MyCustomEvent(resultOfDialog="User Clicked CANCEL")
                self.GetEventHandler().ProcessEvent(event)
            else: # result == wx.ID_OK
                event = MyCustomEvent(resultOfDialog="User Clicked OK")
                self.GetEventHandler().ProcessEvent(event)
            self.Destroy()
        
    app = wx.App(False)
    f2 = wx.Frame(None, title="Frame 1 (for feedback)", size=(400, 350))
    p2 = MyPanel2(f2, sys.stdout)
    f2.Show()
    eventTrackerHandle = CustomEventTracker(sys.stdout, p2.OnResults)
    f1 = MyFrame(None, title="PushEventHandler Tester (deals with on close event)", size=(400, 350))
    f1.PushEventHandler(eventTrackerHandle)
    f1.Show()
    app.MainLoop()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 542k
  • Answers 542k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use: SELECT p.id as a, p.url as b, t.id as… May 17, 2026 at 3:22 am
  • Editorial Team
    Editorial Team added an answer There are two parts to the problem First Issue You… May 17, 2026 at 3:19 am
  • Editorial Team
    Editorial Team added an answer I thought I'd show the regex approach, too. It doesn't… May 17, 2026 at 3:18 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Could someone show me a regular expression that would look through this document and
Could someone recommend any good resources for creating Graphics User Interfaces, preferably in C/C++?
Could someone tell me what the units the SetTimeout(int) method in the ICriteria interface
Could someone write-up a step by step guide to developing a C++ based plugin
Could someone please demystify interfaces for me or point me to some good examples?
Could someone explain to me in simple terms the easiest way to change the
Could someone provide a good description of what a Lambda is? We have a
Could someone supply some code that would get the xpath of a System.Xml.XmlNode instance?
Could someone please tell me which objects types can be tested using Regular Expressions
Could someone please explain the best way to connect to an Interbase 7.1 database

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.