【问题标题】:Return combo box choices as a parameters in wxpython在 wxpython 中将组合框选项作为参数返回
【发布时间】:2018-07-08 20:31:31
【问题描述】:

我正在编写以下代码。 我想将组合框选项作为参数返回并在另一个函数中使用。 由于组合框带有事件处理程序,我找不到在另一个函数中调用它的简单方法。 我的代码如下所示

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() 错过了参数并且程序输出错误 谁能帮帮我,如何处理它

谢谢

【问题讨论】:

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


    【解决方案1】:

    致电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 是通过按钮事件激活的,您的代码中可能不是这种情况。

    【讨论】:

    • 谢谢你,Rolf,虽然它不完全适合我的应用,但它适合并显示了一些前进的方向
    猜你喜欢
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多