问题描述
我正在使用 LC_LIST
样式的 UltimateListCtrl
.我所有列表中的项目都带有复选框.填充列表后,我尝试根据从数据库接收的数据动态检查(打勾)一些项目.但是在列表填充后,我无法检查(打勾)列表中的任何项目.
I am using UltimateListCtrl
with LC_LIST
style. I have all items in list with checkboxes. After populating list, I am trying to check (tick) some items dynamically as per the data I receive from database. But I am not able to check (tick) any item in the list after the list is populated.
我在这里粘贴示例代码来描述问题.
Here I am pasting sample code for describing the issue.
更多信息:我正在Windows XP平台上使用Python 2.7和wxPython 2.8.
More Info: I am using Python 2.7 and wxPython 2.8 on platform Windows XP.
import random
import wx
import wx.lib.agw.ultimatelistctrl as ULC
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, parent=None, title="Tick List Item Checkbox Programatically")
self.panel = wx.Panel(parent=self)
self.sizer = wx.BoxSizer(orient=wx.VERTICAL)
self.panel.SetSizer(self.sizer)
self.list = ULC.UltimateListCtrl(parent=self.panel, agwStyle=wx.LC_LIST)
self.sizer.Add(item=self.list, proportion=1, flag=wx.EXPAND)
for i in range(50):
label = "Item %02d " % (i + 1) + "random " * random.randint(1, 3)
self.list.InsertStringItem(index=i, label=label, it_kind=1)
self.checkItems()
self.Show()
def checkItems(self):
idx = [1, 3, 6, 7, 9, 11, 15, 17, 20]
for i in idx:
# self.list.SetItemState(item=i, state=ULC.ULC_STATE_SELECTED, stateMask=ULC.ULC_MASK_CHECK)
# self.list.SetItemState(item=i, state=ULC.ULC_STATE_SELECTED, stateMask=ULC.ULC_MASK_STATE)
self.list.SetItemState(item=i, state=ULC.ULC_STATE_SELECTED, stateMask=ULC.ULC_STATE_SELECTED)
if __name__ == '__main__':
app = wx.App()
frame = MyFrame()
app.MainLoop()
如何检查(勾选)列表项?
How can I check (tick) list item?
推荐答案
您需要从列表中获取项目,选中它,然后将其保存回列表中:
You need to get the item from the list, check it and then save it back into the list:
def checkItems(self):
idx = [1, 3, 6, 7, 9, 11, 15, 17, 20]
for i in idx:
temp = self.list.GetItem(i, 0)
temp.Check(True)
self.list.SetItem(temp)
这篇关于wxPython UltimateListCtrl以编程方式检查(勾号)listitem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!