1

我正在尝试使用以下代码从 python 列表中填充列表框:

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' 你能告诉我我做错了什么吗?

谢谢

4

1 回答 1

2

您的这一行__init__

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

应该:

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

Python 中的所有实例访问都通过self..

于 2012-07-24T23:56:41.150 回答