2012-07-24 10 views
1

我想從列表中使用Python這個代碼填充列表框:的wxPython:問題上填充一個列表框

import wx 
class ListBoxFrame(wx.Frame): 
def __init__(self): 
    wx.Frame.__init__(self, None, -1, 'List Box Example', size=(500, 500)) 
    panel = wx.Panel(self, -1) 

    btn1 = wx.Button(self, 1, 'List Items', (300, 130)) 
    btn1.Bind(wx.EVT_BUTTON, self.ListItems) 

    listBox1 = wx.ListBox(choices=[], name='listBox1', parent=self, pos=wx.Point(8,   48), size=wx.Size(184, 256), style=0) 

def ListItems(self, event): 
    sampleList = ['zero', 'one', 'two', 'three', 'four', 'five','six', 'seven', 'eight', 'nine', 'ten', 'eleven','twelve', 'thirteen', 'fourteen'] 
    for item in sampleList: 
     self.listBox1.Insert(0,item) 

但我有以下錯誤遭遇: AttributeError的:「ListBoxFrame」對象沒有屬性'listBox1' 你能讓我知道我在做什麼錯嗎?

感謝

回答

2

這條線在__init__

listBox1 = wx.ListBox(..etc..) 

應該是:

self.listBox1 = wx.ListBox(..etc..) 

Python中的所有實例的訪問通過self.工作。

+0

謝謝內德,現在可以工作,但我有兩個問題請你。1 - 爲什麼我們不需要把自己。對於按鈕像btn1 = wx.Button(self,1,'List Items',(300,130))和2我得到另一個錯誤,當我試圖將項目插入列表框爲「return _controls_.ListBox_Insert * args,** kwargs) TypeError:必需的參數'pos'(pos 3)not found「但是在我將Insert方法更改爲Append方法後,它工作正常,請問我知道爲什麼?再次感謝您的完美評論和幫助 – Suffii 2012-07-25 00:07:20