我正在尝试用类外的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
。什么是正确的方法?
答案 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)