开发者

wxPython: How to handle event binding and Show() properly

开发者 https://www.devze.com 2022-12-27 23:14 出处:网络
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.

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()


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

精彩评论

暂无评论...
验证码 换一张
取 消