1

我正在尝试用类外的 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))

我也尝试过全球化 myListbox 内部def __init__(),并使用myWindow.myListbox等,但我仍然得到NameError: global name 'myListbox' is not defined. 这样做的正确方法是什么?

4

1 回答 1

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)
于 2012-10-11T20:26:52.703 回答