7

我是 Python 和 WX 的新手。我创建了一个简单的测试对话框,如下所示,它使用组合框提示用户。我想从我的主程序中的组合框中捕获值。如何从我的主程序中调用它?

这就是我打算如何调用它以显示对话框但当前不从组合框中捕获值:

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

对话框文件的名称是“highlight”。下面是代码:

#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
4

1 回答 1

11

那里有很多对话框示例。这里有一对:

基本上,您需要做的就是实例化对话框,显示它,然后在关闭它之前提取值。典型的做法是这样的:

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

更新:这是一个更成熟的例子:

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()
于 2012-05-09T18:35:43.160 回答