2013-03-29 34 views
0

我有一个字符串数组,说['Hello World', 'Goodbye Universe', Let's go to the mall'],我想把一个ListCtrl我的代码只是打印出数组中的每个索引的某些字母。 我的代码: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]) 

actualrules是数组

+0

变量'我'似乎是一个字符串,而不是索引。你应该得到一些错误(来自'actualrules [i]'),是吗? – Anna

回答

0

你的列表actualrules在字符串中的一个单引号,所以你应该用双引号括起来,如下图所示。

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

在你的for循环i变为列表中的每个项目,那么你正在做只取第一个字母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()