Python WX - Returning user input from wx Dialog

user1314011 picture user1314011 · May 9, 2012 · Viewed 12.5k times · Source

I'm new to Python and WX. I created a simple test dialog shown below that prompts the user with a combobox. I would like to capture the value from the combox in my main program. How do I call it from my main program?

This is how I was purposing to call it that displays the dialog but does not currently capture the value from the combobox:

    import highlight
    highlight.create(self).Show(True)
    a = highlight.OnComboBox1Combobox(self)
    print a

The name of the Dialog file is "highlight". Below is the code:

#Boa:Dialog:Dialog2

import wx

def create(parent):
    return Dialog2(parent)

[wxID_DIALOG2, wxID_DIALOG2COMBOBOX1, wxID_DIALOG2STATICTEXT1, 
] = [wx.NewId() for _init_ctrls in range(3)]

class Dialog2(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DIALOG2, name='', parent=prnt,
              pos=wx.Point(264, 140), size=wx.Size(400, 485),
              style=wx.DEFAULT_DIALOG_STYLE, title='Dialog2')
        self.SetClientSize(wx.Size(384, 447))

        self.comboBox1 = wx.ComboBox(choices=['test1', 'test2'],
              id=wxID_DIALOG2COMBOBOX1, name='comboBox1', parent=self,
              pos=wx.Point(120, 16), size=wx.Size(130, 21), style=0,
              value=u'wining\n')
        self.comboBox1.SetToolTipString(u'comboBox1')
        self.comboBox1.SetLabel(u'wining\n')
        self.comboBox1.Bind(wx.EVT_COMBOBOX, self.OnComboBox1Combobox,
              id=wxID_DIALOG2COMBOBOX1)

        self.staticText1 = wx.StaticText(id=wxID_DIALOG2STATICTEXT1,
              label=u'test', name='staticText1', parent=self, pos=wx.Point(88,
              16), size=wx.Size(19, 13), style=0)

    def __init__(self, parent):
        self._init_ctrls(parent)


        ##print get_selection
        ##print get_selection1

    def OnComboBox1Combobox(self, event):
        get_selection = self.comboBox1.GetValue()
        return get_selection

Answer

Mike Driscoll picture Mike Driscoll · May 9, 2012

There are lots of dialog examples out there. Here are a couple:

Basically, all you need to do is instantiate your dialog, show it and then before you close it, extract the value. The typical way to do it is something like this:

myDlg = MyDialog()
res = myDlg.ShowModal()
if res == wx.ID_OK:
    value = myDlg.myCombobox.GetValue()
myDlg.Destroy()

Update: Here's a more full-fledged example:

import wx

########################################################################
class MyDialog(wx.Dialog):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Dialog.__init__(self, None, title="Dialog")

        self.comboBox1 = wx.ComboBox(self, 
                                     choices=['test1', 'test2'],
                                     value="")
        okBtn = wx.Button(self, wx.ID_OK)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.comboBox1, 0, wx.ALL|wx.CENTER, 5)
        sizer.Add(okBtn, 0, wx.ALL|wx.CENTER, 5)
        self.SetSizer(sizer)

########################################################################
class MainProgram(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Main Program")
        panel = wx.Panel(self)

        btn = wx.Button(panel, label="Open dialog")
        btn.Bind(wx.EVT_BUTTON, self.onDialog)

        self.Show()

    #----------------------------------------------------------------------
    def onDialog(self, event):
        """"""
        dlg = MyDialog()
        res = dlg.ShowModal()
        if res == wx.ID_OK:
            print dlg.comboBox1.GetValue()
        dlg.Destroy()

if __name__ == "__main__":
    app = wx.App(False)
    frame = MainProgram()
    app.MainLoop()