Newbie: saving dialog variables

Jeremy Bowers jerf at jerf.org
Sat May 7 12:39:28 EDT 2005


On Sat, 07 May 2005 15:43:08 +0000, jeff elkins wrote:
> ===============
> import wx
> 
> def create(parent):
>     return vents(parent)
> 
> [wxID_VENTS, wxID_VENTSEXITBUTTON, 
>  wxID_VENTSVENTTYPETEXT,
> [snip]
> 
> ] = [wx.NewId() for _init_ctrls in range(14) ]
> 
> class vents(wx.Dialog):
>     def _init_ctrls(self, prnt):
>         wx.Dialog.__init__(self, id=wxID_VENTS, name=u'prefs', parent=prnt,
>               pos=wx.Point(418, 320), size=wx.Size(321, 285),
>               style=wx.DEFAULT_DIALOG_STYLE, title=u'Ventilator Settings')
>         self.SetClientSize(wx.Size(321, 285))
> 
>         self.exitButton = wx.Button(id=wxID_VENTSEXITBUTTON, label=u'OK',
>               name=u'exitButton', parent=self, pos=wx.Point(60, 250),
>               size=wx.Size(85, 30), style=0)
>         self.exitButton.Bind(wx.EVT_BUTTON, self.OnExitButtonButton,
>               id=wxID_VENTSEXITBUTTON)
> 
>         self.venttypeText = wx.TextCtrl(id=wxID_VENTSVENTTYPETEXT,
>               name=u'venttypeText', parent=self, pos=wx.Point(64, 24),
>               size=wx.Size(144, 25), style=0, value=u'')
> 
> [snip]
> 
>     def __init__(self, parent):
>         self._init_ctrls(parent)
>          
> # build an array of values entered in the dialog
> # return array to calling program
> 
> 
>     def OnExitButtonButton(self, event):
>           self.Close()
> 
> ==================
> 
> The dialog above is called by:
> 
>  def OnVentButtonButton(self, event):
>         dlg = vents.vents(self)
>         try:
>             dlg.ShowModal()
>         finally:
>             dlg.Destroy()

OK, I can't quite directly run this, but assuming you're trying to get the
user to enter some text into the text control, you should be able to add
          print dlg.venttypeText.GetValue()
to print what the user entered; this comes after the try: finally: (i.e.,
on the same indentation as "dlg = vents.vents(self)").

The dialog window is gone, but the widgets and such inside it should
remain until it is garbage collected. (One of the keys to effective UI use
is dissociating the screen representation from the widget data structure
representation; certainly they are related but they are not identical,
generally the widget data structure is around both before and after the
actual display of the widget.)

On an unrelated note, somehow, the dialog should indicate if it was
cancelled or not; you'll need to consult the docs for that. It looks like
you don't want the user to be able to cancel, but watch out for sneaky
cancelling techniques; ESC might be automatically mapped to some sort of
"cancel" by the wx.Dialog class, and the user might also be able to click
on a "close window" button on the resulting dialog box, which may also get
processed as a cancel. I don't think it'll affect your code in this
particular case (it can sometimes), but it's bad UI; there should be a
style that shows a window with no close button on it. (Default may be it,
but I'd be surprised.) That style will probably also not do the default
keyboard mapping, so it should take care of both problems.

If you pull up the dialog and it has no close button and ESC does nothing,
disregard this :-)



More information about the Python-list mailing list