当前位置: 代码迷 >> python >> Ultimatelistctrl:列表索引超出范围
  详细解决方案

Ultimatelistctrl:列表索引超出范围

热度:99   发布时间:2023-06-13 17:00:36.0

我在wxPython中使用UltimateListCtrl。 我可以在上运行演示代码,但是当我尝试对其进行调整时。 或者,甚至将其复制并粘贴到我的程序中,它也会失败:

Traceback (most recent call last):
File "C:\Users\dgunter\Documents\GitHub\TimeJobTracker\timejob.py", line 346, in ArchiveJobs
jobwin = ArchiveJobWindow(self)
File "C:\Users\dgunter\Documents\GitHub\TimeJobTracker\timejob.py", line 87, in __init__
mylist.SetStringItem(0, 1, "Go")
File "C:\Python27\lib\site-packages\wx-3.0-msw\wx\lib\agw\ultimatelistctrl.py", line 11366, in SetStringItem
self._mainWin.SetItem(info)
File "C:\Python27\lib\site-packages\wx-3.0-msw\wx\lib\agw\ultimatelistctrl.py", line 8802, in SetItem
line.SetItem(item._col, item)
File "C:\Python27\lib\site-packages\wx-3.0-msw\wx\lib\agw\ultimatelistctrl.py", line 4095, in SetItem
item = self._items[index]
IndexError: list index out of range

这是我正在使用的代码,您会注意到列表项是从我提供的链接中复制的。 我的实际数据是从我刚刚遗漏的那部分数据库中加载的。

import wx
from wx.lib.agw import ultimatelistctrl as ULC

class ArchiveJobWindow(wx.Frame):
    def __init__(self, parent):
        super(ArchiveJobWindow, self).__init__(parent, title='Archive Unused Jobs', style=wx.CAPTION, size=(650,350))

        self.parent = parent
        self.panel = wx.Panel(self, wx.ID_ANY)
        topSizer = wx.BoxSizer(wx.VERTICAL)
        buttonSizer = wx.BoxSizer(wx.HORIZONTAL)

        font = wx.SystemSettings_GetFont(wx.SYS_DEFAULT_GUI_FONT)
        boldfont = wx.SystemSettings_GetFont(wx.SYS_DEFAULT_GUI_FONT)
        boldfont.SetWeight(wx.BOLD)
        boldfont.SetPointSize(12)


        mylist = ULC.UltimateListCtrl(self.panel, agwStyle=wx.LC_REPORT|wx.LC_VRULES|wx.LC_HRULES|wx.LC_SINGLE_SEL|0x0010, size=(200, 100))

        info = ULC.UltimateListItem()
        info._mask = wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE | wx.LIST_MASK_FORMAT | ULC.ULC_MASK_CHECK
        info._image = []
        info._format = 0
        info._kind = 1
        info._text = "Artist Name"
        mylist.InsertColumnInfo(0, info)

        info = ULC.UltimateListItem()
        info._format = wx.LIST_FORMAT_RIGHT
        info._mask = wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE | wx.LIST_MASK_FORMAT | ULC.ULC_MASK_FONT
        info._image = []
        info._text = "Title"
        info._font = boldfont
        mylist.InsertColumnInfo(1, info)

        info = ULC.UltimateListItem()
        info._mask = wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE | wx.LIST_MASK_FORMAT
        info._format = 0
        info._text = "Genre"
        info._font = font
        info._image = []
        mylist.InsertColumnInfo(2, info)

        mylist.InsertStringItem(0, "Newsboys")
        mylist.SetStringItem(0, 1, "Go")
        mylist.SetStringItem(0, 2, "Rock")

        mylist.InsertStringItem(1, "Puffy")
        mylist.SetStringItem(1, 1, "Bring It!")
        mylist.SetStringItem(1, 2, "Pop")

        mylist.InsertStringItem(2, "Family Force 5")
        mylist.SetStringItem(2, 1, "III")
        mylist.SetStringItem(2, 2, "Crunk")

        mylist.SetColumnWidth(0, 150)
        mylist.SetColumnWidth(1, 200)
        mylist.SetColumnWidth(2, 100)


        cancelbutton = wx.Button(self.panel, id=wx.ID_CANCEL)
        Okbutton = wx.Button(self.panel, id=wx.ID_OK)




        # panelSizer.Add(self.panel, 1, wx.EXPAND)
        buttonSizer.AddStretchSpacer(6)
        buttonSizer.Add(cancelbutton, 1, wx.ALIGN_RIGHT, 0)
        buttonSizer.Add((20,0))
        buttonSizer.Add(Okbutton, 1, wx.ALIGN_RIGHT, 0)
        buttonSizer.AddStretchSpacer(1)

        topSizer.AddStretchSpacer(2)
        topSizer.Add(mylist)
        topSizer.AddStretchSpacer(2)
        topSizer.Add(buttonSizer, 1, wx.ALIGN_RIGHT, 0)
        topSizer.Add((0,20))
        # panelSizer.Add(topSizer, 1, wx.EXPAND)
        self.panel.SetSizer(topSizer)


app = wx.App(False)
window = ArchiveJobWindow(None)
window.Show()


app.MainLoop()

欢迎任何建议。

谢谢

我不明白为什么,但是如果您更改此行,它将使其起作用:

mylist = ULC.UltimateListCtrl(self.panel, agwStyle=wx.LC_REPORT|wx.LC_VRULES|wx.LC_HRULES|wx.LC_SINGLE_SEL|0x0010, size=(200, 100))

对此:

mylist = ULC.UltimateListCtrl(self.panel, agwStyle=ULC.ULC_REPORT, size=(200, 100))

这是因为,为了使示例得以运行,我不得不添加了0x0010

我认为这些例子必须过时了。