在wxPython中使用UltimateListCtrl放置按钮

2 投票
1 回答
3143 浏览
提问于 2025-04-16 03:33

我刚开始学习Python,想在UltimateListCtrl里面添加一个按钮,但一直搞不明白哪里出错了。以下是我的代码:

try:
    from agw import ultimatelistctrl as ULC
except ImportError: # if it's not there locally, try the wxPython lib.
    from wx.lib.agw import ultimatelistctrl as ULC 


self.table = ULC.UltimateListCtrl(self, -1, agwStyle=ULC.ULC_REPORT|
                                            ULC.ULC_HAS_VARIABLE_ROW_HEIGHT)

self.table.InsertColumn(0, "Name")
self.table.InsertColumn(1, "Size")
self.table.InsertColumn(2, "Download")


for i in range(0, len(masterlist)):
    pos = self.table.InsertStringItem(i,str(masterlist[i]['name']))
    self.table.SetStringItem(pos, 1,str(masterlist[i]['size']))
    button = wx.Button(self, id=i, label="Download")
    self.table.SetItemWindow(pos, col=2, wnd=button, expand=True)

masterlist是一个下载项目的列表。

我遇到了这个错误信息:

Traceback (most recent call last):
File "E:\TestApp.py", line 67, in Display
self.table.SetItemWindow(pos, col=5, wnd=button, expand=True)
File "C:\Python27\lib\site-packages\wx-2.8-msw-unicode\wx\lib\agw\ultimatelistctrl.py", line 12961, in SetItemWindow
return self._mainWin.SetItemWindow(item, wnd, expand)
File "C:\Python27\lib\site-packages\wx-2.8-msw-unicode\wx\lib\agw\ultimatelistctrl.py", line 9021, in SetItemWindow
item.SetWindow(wnd, expand)
File "C:\Python27\lib\site-packages\wx-2.8-msw-unicode\wx\lib\agw\ultimatelistctrl.py", line 1863, in SetWindow
mainWin = listCtrl._mainWin
AttributeError: 'MainWindow' object has no attribute '_mainWin'

1 个回答

3

按钮的父级应该是你的 ULC,也就是 self.table

所以把这一行改成:

button = wx.Button(self, id=wx.ID_ANY, label="Download")

改成这样:

button = wx.Button(self.table, id=wx.ID_ANY, label="Download")

根据评论更新:

出于某种原因,如果 ULC 中的任何项目包含小部件,似乎无法使用 DeleteAllItems() 方法删除所有项目,因此请改用 DeleteItem()

def emptyList(self)
    itemCount = self.list.GetItemCount()
    for item in xrange(itemCount):
        self.list.DeleteItem(0)

撰写回答