這段代碼是一個(gè)更大的程序的一部分,該程序使用 google Sheets API 從云數(shù)據(jù)庫中獲取數(shù)據(jù)(不是真正相關(guān),但一點(diǎn)上下文永遠(yuǎn)不會(huì)受到傷害!)我在一個(gè)名為“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 已被另一部分代碼賦值。這一點(diǎn)有效,因?yàn)槲乙呀?jīng)測試過它正在被分配,但是,當(dāng)我在另一個(gè)這樣的文件中調(diào)用“self.tkroot”時(shí)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我收到這個(gè)錯(cuò)誤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 控制臺(tái)中設(shè)置它并且它起作用了,所以我不知道問題是什么。如果有人可以提供幫助,將不勝感激 謝謝!約翰
1 回答

MYYA
TA貢獻(xiàn)1868條經(jīng)驗(yàn) 獲得超4個(gè)贊
您應(yīng)該創(chuàng)建一個(gè)類的實(shí)例,它將在 中創(chuàng)建屬性__init__,self.tkroot是實(shí)例的屬性而不是類:
setupClassroom = SetupClassroom()
print(setupClassroom.tkroot)
希望這會(huì)幫助你。
添加回答
舉報(bào)
0/150
提交
取消