我想使用在类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' –