这段代码是一个更大的程序的一部分,该程序使用 google Sheets API 从云数据库中获取数据(不是真正相关,但一点上下文永远不会受到伤害!)我在一个名为“oop.py”的 python 文件中有这段黑色代码class SetupClassroom: def __init__(self, arraynumber='undefined', tkroot='undefined'): self.arraynumber = arraynumber self.tkroot = tkroot def setarraynumber(self, number): from GUI_Stage_3 import showclassroom self.arraynumber = number print ('set array number:', number) showclassroom() def settkroot(self, tkrootinput): self.tkroot = tkrootinputself.tkroot 已被另一部分代码赋值。这一点有效,因为我已经测试过它正在被分配,但是,当我在另一个这样的文件中调用“self.tkroot”时def showclassroom(): from oop import SetupClassroom username = current_user.username classnumber = getnumberofuserclassrooms(username) if SetupClassroom.arraynumber > classnumber: errorwindow('you are not enrolled in that many classrooms!') else: classtoget = SetupClassroom.arraynumber print('classtoget:', classtoget) root = SetupClassroom.tkroot name_label = Label(root, text=classtoget) getclassroom(username, classtoget)SetupClassroom = SetupClassroom我收到这个错误Exception in Tkinter callbackTraceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 1705, in __call__ return self.func(*args) File "/Users/jonathansalmon/PycharmProjects/Coursework_GUI/GUI_Stage2_better.py", line 176, in <lambda> l0 = ttk.Button(teacher_root, text=button0text, command=lambda: (SetupClassroom.setarraynumber(SetupClassroom, number=button0text), SetupClassroom.settkroot(SetupClassroom, 'teacher_root'))) File "/Users/jonathansalmon/PycharmProjects/Coursework_GUI/oop.py", line 99, in setarraynumber showclassroom() File "/Users/jonathansalmon/PycharmProjects/Coursework_GUI/GUI_Stage_3.py", line 29, in showclassroom root = SetupClassroom.tkrootAttributeError: type object 'SetupClassroom' has no attribute 'tkroot'我尝试在 python 控制台中设置它并且它起作用了,所以我不知道问题是什么。如果有人可以提供帮助,将不胜感激 谢谢!约翰
1 回答
MYYA
TA贡献1868条经验 获得超4个赞
您应该创建一个类的实例,它将在 中创建属性__init__,self.tkroot是实例的属性而不是类:
setupClassroom = SetupClassroom()
print(setupClassroom.tkroot)
希望这会帮助你。
添加回答
举报
0/150
提交
取消