Python WX - 从 wx 对话框返回用户输入 [英] Python WX - Returning user input from wx Dialog

查看:14
本文介绍了Python WX - 从 wx 对话框返回用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

对话框文件的名称是highlight".代码如下:

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

推荐答案

有很多对话框示例.这里有几个:

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

  • The Dialogs of wxPython (Part 1 of 2)
  • http://zetcode.com/wxpython/dialogs/

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

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

这篇关于Python WX - 从 wx 对话框返回用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆