如何将变量从一个类传递到另一个类?

时间:2017-05-23 19:46:43

标签: python class variables inheritance composition

我更新了这个问题。我正在创建两个窗口,并尝试将在A类中创建的变量传递给B类。请您指出我的代码中的问题是什么?这是我的方法:

class Welcome:

       def __init__(self, master, csv_name_sub, csv_name_ses):
            self.entrySes = Entry(self.master, bg="LemonChiffon3")  
            self.entrySub=Entry(self.master,bg="LemonChiffon3")
            self.csv_name_sub = str(self.entrySub.get())
            self.csv_name_ses = str(self.entrySes.get())
            #here users enter some info, they are saved to be used as file name the in following class

class App(Welcome):
       def __init__(self, master):
            Welcome.__init__(self, master, csv_name_ses, csv_name_sub)  #I'm trying use this line to pass the variables from the first class 
            print('session is', self.csv_name_ses)
            print("subject is", self.csv_name_sub)
            self.resultFile = open(
            "C:\\Users\\xxx" + self.csv_name_sub + '_' + self.csv_name_ses +
        '_results.csv', 'w')
       #.....
       #other codes

def main():

    root = Tk()
    myApp = Welcome(root, csv_name_ses, csv_name_sub)
    root.mainloop()

if __name__ == '__main__':
    main()

该程序可以运行,但变量csv_name_sescsv_name_sub不会传递给class App,因为它们不会在课程中打印出来。

我也尝试过如下组合方法,仍然无法解决问题:

class Welcome:
    def __init__(self, master, csv_name_sub, csv_name_ses):
            self.entrySes = Entry(self.master, bg="LemonChiffon3")  
            self.entrySub=Entry(self.master,bg="LemonChiffon3")
            self.csv_name_sub = str(self.entrySub.get())
            self.csv_name_ses = str(self.entrySes.get())

class App:
    def __init__(self, master, csv_name_ses, csv_name_sub):
           self.csv_name_sub = csv_name_sub
           self.csv_name_ses = csv_name_ses
           self.welcomePage = Welcome(master, csv_name_ses, csv_name_sub)
           print('session is', self.welcomePage.csv_name_ses)
           print("subject is", self.welcomePage.csv_name_sub)
    self.resultFile = open(
        "C:\\Users\\xxx + self.welcomePage.csv_name_sub + '_' + self.welcomePage.csv_name_ses +
        '_resultsofPart.csv', 'w')

4 个答案:

答案 0 :(得分:1)

使用默认参数和or初始化self字段:

class Welcome:

    def __init__(self, master, csv_name_sub=None, csv_name_ses=None):
        self.entrySes = Entry(self.master, bg="LemonChiffon3")  
        self.entrySub=Entry(self.master,bg="LemonChiffon3")
        self.csv_name_sub = csv_name_sub or str(self.entrySub.get())
        self.csv_name_ses = csv_name_ses or str(self.entrySes.get())

答案 1 :(得分:0)

问题是Welcome类需要3个args才能实现,而你只提供一个“root”。

答案 2 :(得分:0)

由于Welcome类的构造函数是__init__(self, master, csv_name_sub, csv_name_ses),因此您需要为csv_name_subcsv_name_ses提供一个值来实例化此类。由于您实际上并未使用csv_name_subcsv_name_ses的传递值(请参阅__init__类的Welcome函数中的前4行),这可能是一种解决方案有效:

class Welcome:

   def __init__(self, master):
        self.entrySes = Entry(self.master, bg="LemonChiffon3")  
        self.entrySub=Entry(self.master,bg="LemonChiffon3")
        self.csv_name_sub = str(self.entrySub.get())
        self.csv_name_ses = str(self.entrySes.get())
        #here users enter some info, they are saved to be used as file name the in following class

class App(Welcome):
   def __init__(self, master):
        Welcome.__init__(self, master) 
        print('session is', self.csv_name_ses)
        print("subject is", self.csv_name_sub)
        self.resultFile = open(
        "C:\\Users\\xxx" + self.csv_name_sub + '_' + self.csv_name_ses +
    '_results.csv', 'w')
   #.....
   #other codes

答案 3 :(得分:0)

在使用super class时,要继承父类的var和值,子类需要引用父类,例如Welcome.csv_name_ses

class Welcome:

   def __init__(self, master, csv_name_sub, csv_name_ses):
        self.entrySes = Entry(self.master, bg="LemonChiffon3")  
        self.entrySub=Entry(self.master,bg="LemonChiffon3")
        writeToFile()

   def writeToFile(self):
        Welcome.csv_name_sub = str(self.entrySub.get())
        Welcome.csv_name_ses = str(self.entrySes.get())


class App(Welcome):
   def __init__(self, master, csv_name_ses, csv_name_sub):
        Welcome.__init__(self, master, csv_name_ses, csv_name_sub)
        self.csv_name_sub = Welcome.csv_name_sub
        self.csv_name_ses = Welcome.csv_name_ses  #parent.var

def main():

   root = Tk()
   myApp = Welcome(root, csv_name_ses, csv_name_sub)
   root.mainloop()