wxPython and how to return text entry to main program?

Steve Holden steve at holdenweb.com
Fri Apr 20 11:11:24 EDT 2007


kyosohma at gmail.com wrote:
> On Apr 19, 9:05 pm, Steve Holden <s... at holdenweb.com> wrote:
>> 7stud wrote:
>>> On Apr 19, 1:38 pm, Tyler <hayes.ty... at gmail.com> wrote:
>> [after quoting umpteen lines of code]
>>
>>
>>
>>
>>
>>> You can do this:
>>> ---
>>> class MyFrame(wx.Frame):
>>> ..
>>> ..
>>> ..
>>>     def OnSubmit(self, event):
>>>         globals()["userInput"] = self.txtCtrlName.GetValue()
>>>         self.Close()
>>> app = MyApp()
>>> app.MainLoop()
>>> print userInput
>>> -----
>> Please try to limit your quoting to what's relevant.
>>
>> regards
>>   Steve
>> --
>> Steve Holden       +44 150 684 7255  +1 800 494 3119
>> Holden Web LLC/Ltd          http://www.holdenweb.com
>> Skype: holdenweb    http://del.icio.us/steve.holden
>> Recent Ramblings      http://holdenweb.blogspot.com
> 
> Steve,
> 
> I think Tyler is opening a custom dialog from his main GUI
> application. He should have used "ShowModal" if he was using one of
> wxPython's standard dialog boxes, but since he was creating his own
> custom one he didn't need to, which is why I told him to use the
> self.Close(True) method to destroy the dialog. Then again, maybe I am
> being "wrong-headed" about the whole thing.
> 
> I was trying to come up with the "instance variable" way of storing
> the variables, but I could not think of how to implement it last
> night. Here are some other threads on the topic that I thought were
> interesting:
> 
> http://mail.python.org/pipermail/tutor/2005-May/038648.html
> http://lists.wxwidgets.org/archive/wxPython-users/msg06482.html
> http://www.daniweb.com/techtalkforums/thread60439.html
> 
>
Well, if the whole application is GUI based then yes, the dialog should 
inherit from wx.Dialog not wx.Frame, it should be opened using ShowModal 
either at the click of a button or when the application starts, and 
everything then becomes easy.

The "Run" button event handler should then call EndModal(wx.OK) - there 
can also then be a cancel button that calls EndModal(wx>CANCEL), I 
think, but it's been a while.

The advantage of this is that the dialog still exists after ShowModal() 
returns (which is what the call to EndModal() causes to happen). So 
usually you can do something like this (untested pseudo-code):

    dlg = MyDialog(args_if_needed)
    dlg.ShowModal()
    something = dlg.Control.GetValue()
    somethingElse = dlg.OtherControl.GetValue()
    ... repeat at will ...
    dlg.Destroy()

This is the most often-used pattern for returning data from a dialog, 
but it wasn't easy to see how to adapt that to Tyler's original code. I 
made the mistake of assuming that Tyler knew a bit more about wxPython 
that was actually the case, so I am sorry if the advice was off-target.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb     http://del.icio.us/steve.holden
Recent Ramblings       http://holdenweb.blogspot.com




More information about the Python-list mailing list