在wxpython中将组合框选项作为参数返回

时间:2018-07-08 20:31:32

标签: python-3.x combobox event-handling wxpython

我正在研究以下代码。 我想返回组合框选择作为参数并在另一个函数中使用。 由于组合框带有事件处理程序,因此我找不到在其他函数中调用它的简便方法。 我的代码如下所示

self.combo_box_product = wx.ComboBox(self.panel_1, wx.ID_ANY, choices=["one", "two", "three", "OTHERS"], style=wx.CB_DROPDOWN | wx.CB_READONLY | wx.TE_PROCESS_ENTER)
self.Bind(wx.EVT_COMBOBOX, self.OnCombo, self.combo_box_product)

def OnCombo(self, event):  
    product = self.combo_box_product.GetValue()
    return product
    event.Skip()

我想调用另一个函数,如下所示:

def func(self):
    x=self.OnCombo()
    y=x

但是您已经猜错了OnCombo()错过了参数,程序输出了错误 有人可以帮我,如何处理

谢谢

1 个答案:

答案 0 :(得分:0)

致电self.OnCombo(None)

import wx

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None, title="Window", size =(650,350))
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.combo_box_product = wx.ComboBox(self, wx.ID_ANY, choices=["one", "two", "three", "OTHERS"], style=wx.CB_DROPDOWN | wx.TE_PROCESS_ENTER)
        self.Bind(wx.EVT_COMBOBOX, self.OnCombo, self.combo_box_product)
        sizer.Add(self.combo_box_product, 0, wx.ALL, 10)
        button = wx.Button(self, -1, "Function")
        self.Bind(wx.EVT_BUTTON, self.func, button)
        sizer.Add(button, 0, wx.ALL, 10)
        self.SetSizer(sizer)
        self.Show()

    def OnCombo(self, event):
        product = self.combo_box_product.GetValue()
        return product

    def func(self,event):
        x=self.OnCombo(None)
        print (x)

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

本示例使用func(self,event),因为func是通过按钮事件激活的,在您的代码中可能并非如此。