在类外使用wxPython元素?

时间:2012-10-11 20:23:56

标签: python wxpython

我正在尝试用类外的wx元素做一些事情:

def doStuff(uselessVariableIsUseless):
    myListbox.Set(myList)

class myWindow(wx.Frame):
    def __init__(self, parent, id):
        myListbox = wx.ListBox(parent = self, pos = (320, 30), size = (300, 500))

我还尝试在def __init__()内全局化myListbox,并使用myWindow.myListbox等,但我仍然得到NameError: global name 'myListbox' is not defined。什么是正确的方法?

1 个答案:

答案 0 :(得分:2)

您需要分配到self.myListbox以使其成为实例的属性。然后,您可以将实例传递给doStuff方法,并通过<object>.myListbox访问它:

def doStuff(win):
    win.myListbox.Set(myList)

class myWindow(wx.Frame):
    def __init__(self, parent, id):
        self.myListbox = wx.ListBox(parent = self, pos = (320, 30), size = (300, 500))

my_win = myWindow(0, 0)
doStuff(my_win)