0

我有一个字符串数组说['Hello World', 'Goodbye Universe', Let's go to the mall'],我想放入 ListCtrl 怎么我的代码只打印出数组中每个索引的某些字母。我的代码:

self.list = wx.ListCtrl(panel,size=(1000,1000))
self.list.InsertColumn(0,'Rules')
for i in actualrules:
    self.list.InsertStringItem(sys.maxint, i[0])

实际规则是数组

4

1 回答 1

1

您的列表 actualrules 在其中一个字符串中有一个单引号,因此您应该用双引号将其括起来,如下所示。

        actualrules = ['Hello World', 'Goodbye Universe',
                   "Let's go to the mall"]

在您的 for 循环中,我成为列表的每个项目,然后您通过执行 i[0] 仅获取第一个字母

以下是您的列表的一个工作示例

import sys
import wx


class TestFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(TestFrame, self).__init__(*args, **kwargs)

        actualrules = ['Hello World', 'Goodbye Universe',
                       "Let's go to the mall"]

        panel = wx.Panel(self)
        self.list = wx.ListCtrl(panel, size=(1000, 1000), style=wx.LC_REPORT)
        self.list.InsertColumn(0, 'Rules')
        for i in actualrules:
            self.list.InsertStringItem(sys.maxint, i)

        pSizer = wx.BoxSizer(wx.VERTICAL)
        pSizer.Add(self.list, 0, wx.ALL, 5)
        panel.SetSizer(pSizer)

        vSizer = wx.BoxSizer(wx.VERTICAL)
        vSizer.Add(panel, 1, wx.EXPAND)
        self.SetSizer(vSizer)


if __name__ == '__main__':
    wxapp = wx.App(False)
    testFrame = TestFrame(None)
    testFrame.Show()
    wxapp.MainLoop()
于 2013-03-29T14:32:36.043 回答