python - 带有 CheckListBox 弹出窗口的 ComboCtrl - EVT_CHECKLISTBOX 不起作用

标签 python user-interface combobox wxpython

在我上一篇文章之后,我发现我可以将 ComboCtrl 与 Checklistbox Popup 结合起来。到目前为止,一切都很好。现在我尝试找出为什么 EVT_CHECKLISTBOX 无法正常工作。是不是我绑定(bind)的方式不对?

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

最佳答案

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

关于python - 带有 CheckListBox 弹出窗口的 ComboCtrl - EVT_CHECKLISTBOX 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55082189/

相关文章:

ios - Swift 4 One View Controller 两个 View 隐藏导航栏

c++ - 有人用过smartwin(一个Windows C++ GUI OS库)吗?

javascript - 等待ajax请求完成

java - 具有自定义对象的可编辑组合框重写下拉列表中的项目

.net - 将 FontStyles 和 FontWeights 绑定(bind)到 WPF ComboBox

Python 等价于 Perl 的 while (<>) {...}?

python - 解释 scipy 的 line_search 中的 amax 参数

python - 如何找到 cp 和 cp_p_g 在 M 轴上的交点?

python - 在 Flask 中使用隐藏字段_method 更改请求方法

javascript - 使用 AngularJS 打造原生桌面/移动应用程序?