我想使用在類B中的類A中定義的變量。基本上我需要類A中的用戶條目是我保存的數據的文件名在B類下面是我的代碼:在類A中使用變量在Python中的類B
class A(object):
def __init__(self, master):
self.master = master
self.labelSub=Label(self.master, text="Participant No.") #where users their name
self.entrySub=Entry(self.master,bg="grey")
A.csv_name_sub = str(self.entrySub.get()) #save users entry
class B(A):
def __init__(self, master):
self.master = master
A.csv_name_sub = str(self.entrySub.get())
self.resultFile = open("/Users/Desktop/" + A.csv_name_sub +
'_results.csv', 'w') #use the users entry as the name of the csv file I save
但錯誤告訴我:「AttributeError錯誤:‘B’對象有沒有屬性‘entrySub’」。你能幫我解決嗎?謝謝!!
通過在變量的類的開頭添加'Global'使其成爲一個全局變量。像'全球A' –