Search code examples
pythonwxpythonwxwidgets

wxpython - How to refresh listbox from outside of class?


class ListCtrl(wx.ListCtrl):

    def __init__(self, parent):
        super(ListCtrl, self).__init__(parent,size=(1200,700))

    def delete_items(self):
        self.DeleteAllItems()

class One(wx.Panel):
    b =wx.Button()
    b.bind(**Listbox.delete_items**)


class Two(wx.Panel):
    self.lb = Listbox(self)
  1. *In my application, I have two panels.. class One represents the sidebar panel which contains buttons. Class Two represents the main panel which contains the listbox.

  2. How do I call a function via a button (in this case to delete items from a listbox ) whose parent belongs to another class (Two)?*


Solution

  • one way you could do this is with pub sub

    from wx.lib.pubsub import Publisher
    pub = Publisher()
    all_options = "One Two Three".split()
    class One(wx.Panel):
         def on_delete_button(self,evt):
             all_options.pop(0)
             pub.sendMessage("update.options",
    
    class Two(wx.Panel):
         def __init__(self,*args,**kwargs):
            self.lb = Listbox(self)
            self.lb.SetItems(all_options)
            pub.subscribe("update.options",lambda e:self.lb.SetItems(e.data))
    

    that said there are many many ways to accomplish this