gpt4 book ai didi

Python 和使用 ListCtrl 进行多选

转载 作者:行者123 更新时间:2023-12-01 05:34:07 29 4
gpt4 key购买 nike

我的应用程序中有一个自定义 listctrl,我希望能够选择多行(并取消选择),就像在列表框中所做的那样。目前我有一个 listctrl,我可以获取单个选择;但是,一旦我单击 listctrl 中的另一行,即之前的“取消突出显示”。我希望它保持突出显示状态,除非我再次单击它 - 这样用户就知道他/她选择了哪些行(与列表框的工作方式完全相同)。我尝试将 wx.LC_MULTIPLE_SEL 添加到 listctrl 行,但这不起作用。

帮忙?谢谢!

我从 Mike Driscoll 的以下站点中获取了此示例 Python Blog 。我适应了它来获取行选择。本质上,我选择一行并将索引附加到名为 InfoList 的列表中。当我选择一个新行时,它会正确附加,但我希望该行在实际列表中保持“突出显示”。然后,如果我在 listctrl 中再次选择某个项目,我可以添加另一行以从列表中删除该项目。

import wx
import wx.lib.mixins.listctrl as listmix

InfoList = []

musicdata = {
0 : ("Bad English", "The Price Of Love", "Rock"),
1 : ("DNA featuring Suzanne Vega", "Tom's Diner", "Rock"),
2 : ("George Michael", "Praying For Time", "Rock"),
3 : ("Gloria Estefan", "Here We Are", "Rock"),
4 : ("Linda Ronstadt", "Don't Know Much", "Rock"),
5 : ("Michael Bolton", "How Am I Supposed To Live Without You", "Blues"),
6 : ("Paul Young", "Oh Girl", "Rock"),
}

########################################################################
class TestListCtrl(wx.ListCtrl):

#----------------------------------------------------------------------
def __init__(self, parent, ID=wx.ID_ANY, pos=wx.DefaultPosition,
size=wx.DefaultSize, style=0):
wx.ListCtrl.__init__(self, parent, ID, pos, size, style)

########################################################################
class TestListCtrlPanel(wx.Panel, listmix.ColumnSorterMixin):

#----------------------------------------------------------------------
def __init__(self, parent):
wx.Panel.__init__(self, parent, -1, style=wx.WANTS_CHARS)

self.list_ctrl = TestListCtrl(self, size=(-1,100),
style=wx.LC_REPORT
|wx.BORDER_SUNKEN
|wx.LC_SORT_ASCENDING
)
self.list_ctrl.InsertColumn(0, "Artist")
self.list_ctrl.InsertColumn(1, "Title", wx.LIST_FORMAT_RIGHT)
self.list_ctrl.InsertColumn(2, "Genre")

items = musicdata.items()
index = 0
for key, data in items:
self.list_ctrl.InsertStringItem(index, data[0])
self.list_ctrl.SetStringItem(index, 1, data[1])
self.list_ctrl.SetStringItem(index, 2, data[2])
self.list_ctrl.SetItemData(index, key)
index += 1

# Now that the list exists we can init the other base class,
# see wx/lib/mixins/listctrl.py
self.itemDataMap = musicdata
listmix.ColumnSorterMixin.__init__(self, 3)
self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColClick, self.list_ctrl)

### I ADDED THIS ###
self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnSelectMustHave, self.list_ctrl)

sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5)
self.SetSizer(sizer)

### I ADDED THIS ###
def OnSelectMustHave(self,event):
info = event.GetData()
InfoList.append(info)
print info,InfoList

#----------------------------------------------------------------------
# Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
def GetListCtrl(self):
return self.list_ctrl

#----------------------------------------------------------------------
def OnColClick(self, event):
print "column clicked"
event.Skip()

########################################################################
class MyForm(wx.Frame):

#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "List Control Tutorial")

# Add a panel so it looks the correct on all platforms
panel = TestListCtrlPanel(self)

#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()

最佳答案

很恶心,但这会做你想要的

def OnSelectMustHave(self,event):        
info = event.GetData()
InfoList.append(info)
def do_select():
map(lambda x:self.list_ctrl.Select(x,1) ,set(InfoList))

wx.CallLater(100,do_select)
print info,InfoList
return

但是,由于您在 InfoList 中有选定元素的列表,因此类似下面的解决方案可能会起作用

def OnSelectMustHave(self,event):        
info = event.GetData()
InfoList.append(info)
map(lambda x:self.list_ctrl.SetItemBackgroundColour(x,wx.LIGHT_GREY) ,set(InfoList))
print info,InfoList
return

关于Python 和使用 ListCtrl 进行多选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19551420/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com