2013-03-29 68 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()