Python WX - Returning user input from wx Dialog

点点圈 提交于 2019-11-30 20:39:06

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()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!