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

  • SEARCH
  • Home
  • 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 768751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:13:40+00:00 2026-05-14T18:13:40+00:00

I’m just starting out with wxPython and this is what I would like to

  • 0

I’m just starting out with wxPython and this is what I would like to do:

a) Show a Frame (with Panel inside it) and a button on that panel.
b) When I press the button, a dialog box pops up (where I can select from a choice).
c) When I press ok on dialog box, the dialog box should disappear (destroyed), but the original Frame+Panel+button are still there.
d) If I press that button again, the dialog box will reappear.

My code is given below. Unfortunately, I get the reverse effect. That is,

a) The Selection-Dialog box shows up first (i.e., without clicking on any button since the TopLevelframe+button is never shown).

b) When I click ok on dialog box, then the frame with button appears.

c) Clicking on button again has no effect (i.e., dialog box does not show up again).

What am I doing wrong ? It seems that as soon as the frame is initialized (even before the .Show() is called), the dialog box is initialized and shown automatically.

I am doing this using Eclipse+Pydev on WindowsXP with Python 2.6

============File:MainFile.py===============

import wx
import MyDialog   #This is implemented in another file: MyDialog.py

class TopLevelFrame(wx.Frame):

    def __init__(self,parent,id):    
        wx.Frame.__init__(self,parent,id,"Test",size=(300,200))
        panel=wx.Panel(self)
        button=wx.Button(panel, label='Show Dialog', pos=(130,20), size=(60,20))

        # Bind EVENTS --> HANDLERS. 
        button.Bind(wx.EVT_BUTTON, MyDialog.start(self))  


# Run the main loop to start program.         
if __name__=='__main__':
    app=wx.PySimpleApp()    
    TopLevelFrame(parent=None, id=-1).Show()  
    app.MainLoop()

============File:MyDialog.py===============

import wx

def start(parent):
    inputbox = wx.SingleChoiceDialog(None,'Choose Fruit', 'Selection Title',
                                     ['apple','banana','orange','papaya'])
    if inputbox.ShowModal()==wx.ID_OK:
        answer = inputbox.GetStringSelection()
        inputbox.Destroy()
  • 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-14T18:13:40+00:00Added an answer on May 14, 2026 at 6:13 pm

    There are a number of ways to do this, but to make the least number of changes to your code,

    Change def start(parent): to

    def start(parent, evt):
    

    And change button.Bind(wx.EVT_BUTTON, MyDialog.start(self)) to

        button.Bind(wx.EVT_BUTTON, lambda evt: MyDialog.start(self, evt))
    

    That is, the second argument in Bind needs to be a function that takes and event, and you need to create the dialog box when the button is clicked. lambda makes this a function that also takes parent and evt (you can also use functools.partial for version >2.5), and then when the button is clicked, start will be called to create the dialog.

    I’m not quite sure what’s going on in your code, but it seems that you’re calling start and creating the dialog in your initial call to Bind, and then passing the return value from start, None to Bind.

    Note 1
    In more detail, the reason to use the lambda here is that Bind should have a form like Bind(event, handler) where event is a wx.PyEventBinder, like wx.EVT_BUTTON, and handler is a function like foo(evt) where evt is a wx.Event or wx.CommandEvent. (There’s no recursion here, as you’re just saying what to do when something happens, but that thing hasn’t happened yet, so the event hasn’t been created. When the event does happen, it will be represented by a wx.Event, which will have information about the event, like where the mouse was when it was clicked, etc.)

    Note 2
    In my answer I tried to answer your question with minimal changes as I thought that would be easiest. Maybe the code below is more clear (and maybe it’s generally clearest to handle events within the widget that creates them):

    def start2(parent):
        inputbox = wx.SingleChoiceDialog(parent,'Choose Fruit', 'Selection Title',
                                         ['apple','banana','orange','papaya'])
        if inputbox.ShowModal()==wx.ID_OK:
            answer = inputbox.GetStringSelection()
        inputbox.Destroy()
    
    class TopLevelFrame2(wx.Frame):
    
        def __init__(self,parent,id):    
            wx.Frame.__init__(self,parent,id,"Test",size=(300,200))
            panel=wx.Panel(self)
            button=wx.Button(panel, label='Show Dialog', pos=(130,20), size=(60,20))
    
            # Bind EVENTS --> HANDLERS. 
            button.Bind(wx.EVT_BUTTON, self.OnClick)  
    
        def OnClick(self, evt):
            start2(self)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.