Newbie: saving dialog variables

J

jeff elkins

Howdy,

I've written a program that calls an imported dialog to gather some needed
input. What's the common method for passing that data back to the caller? I've
tried a 'return data' prior to self.Close() ... all that happens then is the
dialog won't close. I'm sure this is obvious, but this newbie's stuck!

Thanks,

Jeff
 
J

Jeremy Bowers

Howdy,

I've written a program that calls an imported dialog to gather some needed
input. What's the common method for passing that data back to the caller? I've
tried a 'return data' prior to self.Close() ... all that happens then is the
dialog won't close. I'm sure this is obvious, but this newbie's stuck!

Thanks,

Jeff

In general, the dialog is an instance of a class. Once the dialog closes,
the window should be gone but the instance variable should still be around.
Common practice is to put the relevant data in the dialog instance member
for retrieval after closing. In certain cases, the method used to invoke
the dialog will return the relevant value, but this is somewhat limiting.
In even more rare cases, the dialog will be popped up by a function,
giving no direct reference to the dialog at any point, and the value is
returned by the function; this is generally limited to the "Yes/No/Cancel"
style dialog or its simpler bretheren ("OK/Cancel" and "OK").

I'm assuming that last one is not the case.

To be more directly helpful, we'd need more data, ideally a code snippet
fully demonstrating the problem (i.e., a runnable program). But at a bare
minimum, we'd need to know where this dialog came from. Tk? PyGTK?
wxPython? Some curses library? MFC?
 
J

jeff elkins

In general, the dialog is an instance of a class. Once the dialog closes,
the window should be gone but the instance variable should still be around.
Common practice is to put the relevant data in the dialog instance member
for retrieval after closing. In certain cases, the method used to invoke
the dialog will return the relevant value, but this is somewhat limiting.
In even more rare cases, the dialog will be popped up by a function,
giving no direct reference to the dialog at any point, and the value is
returned by the function; this is generally limited to the "Yes/No/Cancel"
style dialog or its simpler bretheren ("OK/Cancel" and "OK").

I'm assuming that last one is not the case.

To be more directly helpful, we'd need more data, ideally a code snippet
fully demonstrating the problem (i.e., a runnable program). But at a bare
minimum, we'd need to know where this dialog came from. Tk? PyGTK?
wxPython? Some curses library? MFC?

Jeremy,

The dialog is from wxPython, generated using Boa Constructor. I'm cutting out
hunks to try to be concise...

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


Thanks again,

Jeff
 
J

Jeremy Bowers

===============
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 :)
 
J

jeff elkins

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)").

Thanks! That did the trick.

Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,238
Messages
2,571,193
Members
47,830
Latest member
ZacharySap

Latest Threads

Top