【问题标题】:wxpython - How to refresh listbox from outside of class?wxpython - 如何从课堂外刷新列表框?
【发布时间】:2015-07-14 18:44:53
【问题描述】:
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. *在我的应用程序中,我有两个面板。类 One 代表包含按钮的侧边栏面板。第二类代表包含列表框的主面板。

  2. 如何通过其父级属于另一个类 (Two) 的按钮(在本例中为从列表框中删除项目)调用函数?*

【问题讨论】:

    标签: python wxpython wxwidgets


    【解决方案1】:

    你可以做到这一点的一种方法是使用 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))
    

    说有很多方法可以做到这一点

    【讨论】:

    • 是的,但 pubsub 可能是最好的方式......或者无论如何是最干净的方式之一
    • 这正是我想要的。我不得不使用“from wx.lib.pubsub import setuparg1”和“from wx.lib.pubsub import pub”来避免导入错误。
    猜你喜欢
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-04
    • 1970-01-01
    相关资源
    最近更新 更多