wxWidgets - 如何在wxpython中由comboctrl小部件创建的ListBox中添加滚动条

时间:2016-09-22 10:13:04

标签: wxpython wxwidgets

我使用wxcomboctrl创建了下拉列表。

self.Worker = wx.combo.ComboCtrl(self, -1, size=(250, -1), style=wx.CB_DROPDOWN)
self.testWorkloadComboCtrl = self.ListCtrlComboPopup()
self.worker.SetPopupControl(self.workerComboCtrl)


class ListCtrlComboPopup(wx.ListCtrl, wx.combo.ComboPopup):

    def __init__(self):
        self.PostCreate(wx.PreListCtrl())

        # Also init the ComboPopup base class.
        wx.combo.ComboPopup.__init__(self)


    def AddItem(self, txt):
        for t in txt:
            self.InsertStringItem(self.GetItemCount(), t)


    def OnMotion(self, evt):

        item, flags = self.HitTest(evt.GetPosition())
        if item >= 0:
            self.Select(item)
            self.curitem = item


    def OnLeftDown(self, evt):

        self.value = self.curitem
        self.Dismiss()


    # The following methods are those that are overridable from the
    # ComboPopup base class.

    def Init(self):
        """ This is called immediately after construction finishes.  You can
        use self.GetCombo if needed to get to the ComboCtrl instance. """

        self.value = -1
        self.curitem = -1


    def Create(self, parent):
        """ Create the popup child control. Return True for success. """

        wx.ListCtrl.Create(self, parent,
                       style=wx.LC_LIST|wx.LC_SINGLE_SEL|wx.SIMPLE_BORDER)
        self.Bind(wx.EVT_MOTION, self.OnMotion)
        self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)

        return True


    def GetControl(self):
        """ Return the widget that is to be used for the popup. """

        return self

    def SetStringValue(self, val):
        """ Called just prior to displaying the popup, you can use it to
    'select' the current item. """

        idx = self.FindItem(-1, val)

        if idx != wx.NOT_FOUND:
            self.Select(idx)


    def GetStringValue(self):
        """ Return a string representation of the current item. """

        if self.value >= 0:
            return self.GetItemText(self.value)

        return ""


    def OnPopup(self):
        """ Called immediately after the popup is shown. """

        wx.combo.ComboPopup.OnPopup(self)


    def OnDismiss(self):
        " Called when popup is dismissed. """

        wx.combo.ComboPopup.OnDismiss(self)

显示带有水平滚动条的2或3列项目的下拉列表。

但我希望显示一个包含水平和垂直滚动条的项目列表。

我搜索了comboctrl属性,但仍然不清楚comboctrl。

任何人都可以就此提出建议。

1 个答案:

答案 0 :(得分:0)

尝试使用QPainter样式而不是wx.LC_REPORT。您还需要像使用wx.LC_LIST样式wx.LC_REPORT一样创建列。