wx.ListCtrl不显示由SetStringItem添加的字符串

时间:2012-02-26 22:47:24

标签: python wxpython

任何帮助都表示赞赏这让我疯了

import wx

class Frame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, -1)

        # dp = DownloadPanel(self)
        # dl = dp.download_list
        dl = wx.ListCtrl(self, -1, style=wx.LC_REPORT)
        dl.InsertColumn(0, "File")
        dl.InsertColumn(1, "Progress")
        dl.InsertColumn(2, "State")
        dl.SetStringItem(0, 0, "FILE")
        dl.SetStringItem(0, 1, "PERCENT")
        dl.SetStringItem(0, 2, "STATE")
        dl.SetStringItem(1, 0, "FILE")
        dl.SetStringItem(1, 1, "PERCENT")
        dl.SetStringItem(1, 2, "STATE")
        dl.SetStringItem(2, 0, "FILE")
        dl.SetStringItem(2, 1, "PERCENT")
        dl.SetStringItem(2, 2, "STATE")
        dl.SetStringItem(3, 0, "FILE")
        dl.SetStringItem(3, 1, "PERCENT")
        dl.SetStringItem(3, 2, "STATE")

        self.Refresh()
        dl.Refresh()
        self.Update()
        dl.Update()
        self.Layout()
        dl.Layout()

if __name__ == "__main__":
    a = wx.App(False)
    f = Frame()
    f.Show()
    a.MainLoop()

好的,当我运行这个项目没有显示时,我做错了什么?我正在添加这些项目。 AHHHH!

1 个答案:

答案 0 :(得分:3)

使用InsertStringItem开始新行:

import wx
import sys

class Frame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, size=(380, 230))

        dl = wx.ListCtrl(self, -1, style=wx.LC_REPORT)
        dl.InsertColumn(0, 'File')
        dl.InsertColumn(1, 'Progress')
        dl.InsertColumn(2, 'State')

        for row in range(3):
            labels = [l+str(row) for l in "FILE PERCENT STATE".split()]
            # sys.maxint inserts at the end of the list
            index = dl.InsertStringItem(sys.maxint, labels[0])
            dl.SetStringItem(index, 1, labels[1])
            dl.SetStringItem(index, 2, labels[2])

        self.Show(True)

app = wx.App()
Frame(None)
app.MainLoop()

产量

enter image description here

我通过将您的代码与找到的样本here进行比较来了解这一点。示例here也可以为您提供帮助。