I'm trying to update a variable in classA and used the updated info in classB. Hence I have two questions: 1) How to update class variable in instance method? 2) How to use inherit variables from parent class?
The main idea of this program is that user enters the name, and the name var, its value csv_name_sub will be save and updated and used in class B. (i.e. used as file name)
classA(object):
def __init__(self, master, csv_name_sub):
self.entrySub = Entry(self.master,bg="grey") #user enters sth
button1 = Button(self.master,text='NEXT', command=self.gotoPage1)
def writeToFile(self):
self.csv_name_sub = str(self.entrySub.get()) #save value to var
def gotoPage1(self):
self.writeToFile()
root2=Toplevel(self.master)
self.instPage1=classB(root2)
classB(classA):
def __init__(self, master, csv_name_sub):
classA.__init__(self, master, csv_name_sub)
print(self.csv_name_sub)
self.resultFile = open(
"/Users/" + self.csv_name_sub,'w')
self.resultFileWrite = csv.writer(self.resultFile)
def main():
root = Tk()
myApp = classA(root, csv_name_sub)
root.mainloop()
But the error is:
myApp = classA(root, csv_name_sub)
NameError: name 'csv_name_sub' is not defined
I understand that csv_name_sub is created in the parent class, and its value is not inherited in the child class. But how can I access the variable value in the child class? Because the value of csv_name_sub is determined by what users entered in the parent class, I can't define it in the child class by myself.
Thanks for you help!
csv_name_subis no where defined in the main. likeroot = Tk()you should definecsv_name_sub.ClassBobject.csv_name_subcreated in classA in classB. I understand that its value will not be inherited in classB, because it's not created in classB. So how can I find a way to usecsv_name_sub's value (created in parent class) in child class?