Search code examples
python-3.xcomboboxevent-handlingwxpython

Return combo box choices as a parameters in wxpython


i am working on following piece of code. i want to return combo box choices as parameters and use in another function. As combo box comes up with event handler i don't find an easy way to call it in another function. my code looks like following

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

and i want call in another function as below:

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

but as you already guess mistake is OnCombo() misses argument and program outputs error Can someone help me, how to dealt with it

Thanks


Solution

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

    N.B. This example uses func(self,event) because func is activated via a button event, this may not be the case in your code.