【问题标题】:ComboCtrl with a CheckListBox popup - EVT_CHECKLISTBOX doesn't work带有 CheckListBox 弹出窗口的 ComboCtrl - EVT_CHECKLISTBOX 不起作用
【发布时间】:2019-03-09 21:36:07
【问题描述】:

在上一篇文章之后,我发现我可以将 ComboCtrl 与 Checklistbox Popup 结合使用。到现在为止还挺好。现在我试图弄清楚为什么 EVT_CHECKLISTBOX 不能正常工作。我是不是绑定错了?

self.lc.Bind(wx.EVT_CHECKLISTBOX, self.OnSelect)

还有:

  • 如何使弹出窗口适合内容?目前它非常庞大
  • 如何更改不再填满整个窗口的组合框?

到目前为止,这是我的代码:

import wx
import wx.stc
from wx.lib.mixins.listctrl import CheckListCtrlMixin, ListCtrlAutoWidthMixin

class CheckListCtrl(wx.ListCtrl, CheckListCtrlMixin, ListCtrlAutoWidthMixin):
    def __init__(self, parent):
        wx.ListCtrl.__init__(self, parent, wx.ID_ANY, style=wx.LC_REPORT |
                wx.SUNKEN_BORDER)
        CheckListCtrlMixin.__init__(self)
        ListCtrlAutoWidthMixin.__init__(self)  

class ListViewComboPopup(wx.ComboPopup):
    def __init__(self):
        wx.ComboPopup.__init__(self)
        self.lc = None

    def AddItem(self, txt):
        self.lc.InsertItem(0, txt)

    def OnSelect(self, event):
        print("Working fine!")

    def Init(self):
        self.value = -1
        self.curitem = -1

    def Create(self, parent):
        self.lc = CheckListCtrl(parent)
        self.lc.InsertColumn(0, '', width=90)

        self.lc.Bind(wx.EVT_CHECKLISTBOX, self.OnSelect)
        return True

    def GetControl(self):
        return self.lc

    def OnPopup(self):
        wx.ComboPopup.OnPopup(self)

class MyForm(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="Popup Menu Tutorial")

        comboCtrl = wx.ComboCtrl(self, wx.ID_ANY, "")        
        popupCtrl = ListViewComboPopup()
        comboCtrl.SetPopupControl(popupCtrl)

        popupCtrl.AddItem("Test 1")
        popupCtrl.AddItem("Test 2")
        popupCtrl.AddItem("Test 3") 

if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm().Show()
    app.MainLoop()

【问题讨论】:

    标签: python user-interface combobox wxpython


    【解决方案1】:

    OnCheckItem 由 CheckListCtrlMixin 调用。
    将面板添加到框架并将 ComboCtrl 的父级设置为面板
    改变 ComboPopup 的方法 GetAdjustedSize 返回值来改变大小

    import wx
    import wx.stc
    from wx.lib.mixins.listctrl import CheckListCtrlMixin, ListCtrlAutoWidthMixin
    
    
    class CheckListCtrl(wx.ListCtrl, CheckListCtrlMixin, ListCtrlAutoWidthMixin):
    
        def __init__(self, parent):
            wx.ListCtrl.__init__(self, parent, wx.ID_ANY, style=wx.LC_REPORT | 
                    wx.SUNKEN_BORDER)
            CheckListCtrlMixin.__init__(self)
            ListCtrlAutoWidthMixin.__init__(self)
            self.SetSize(-1, -1, -1, 50)
    
        def OnCheckItem(self, index, flag):
            item = self.GetItem(index)
            if flag:
                what = "checked"
            else:
                what = "unchecked"
    
            print(f'{item.GetText()} - {what}')
    
    
    class ListViewComboPopup(wx.ComboPopup):
    
        def __init__(self):
            wx.ComboPopup.__init__(self)
            self.lc = None
    
        def AddItem(self, txt):
            self.lc.InsertItem(0, txt)
    
    
        def Init(self):
            self.value = -1
            self.curitem = -1
    
        def Create(self, parent):
            self.lc = CheckListCtrl(parent)
            self.lc.InsertColumn(0, '', width=90)
            return True
    
        def GetControl(self):
            return self.lc
    
        def OnPopup(self):
            wx.ComboPopup.OnPopup(self)
    
        def GetAdjustedSize(self, minWidth, prefHeight, maxHeight):
            return wx.ComboPopup.GetAdjustedSize(
                self, minWidth, 110, maxHeight)
    
    
    class MyForm(wx.Frame):
    
        def __init__(self):
            wx.Frame.__init__(self, None, title="Popup Menu Tutorial")
            panel = wx.Panel(self)
    
            comboCtrl = wx.ComboCtrl(panel, wx.ID_ANY, "Select filter")    
            popupCtrl = ListViewComboPopup()
            comboCtrl.SetPopupControl(popupCtrl)
    
            popupCtrl.AddItem("Test 1")
            popupCtrl.AddItem("Test 2")
            popupCtrl.AddItem("Test 3") 
    
    
    if __name__ == "__main__":
        app = wx.App(False)
        frame = MyForm().Show()
        app.MainLoop()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-03
      相关资源
      最近更新 更多