2017-05-23 370 views
0

我更新了這個問題。我創建了兩個窗口,並嘗試將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,因爲它們不是在課堂上打印出來。

我也嘗試過的Composition如下的方法,仍然不能解決問題:

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') 

謝謝!

+0

接受它們作爲參數(有幾種方法)並調用基類。您還需要創建一個實例'App'*,它是*'Welcome'的子類 - 在子類場景中只創建一個* instance *,並且想到「傳遞給類」會產生混淆。或者,歡迎您成爲App的子類更合適?或者,使用Composition可能會更簡單,更容易混淆? – user2864740

+0

感謝您的評論和建議。我試圖使用組合,但仍不能使用子類中的變量。我已經更新了這個問題,請介意幫我看一下嗎?謝謝 – Kay

回答

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() 
0

問題是歡迎類需要3個參數實例化,而您只給出一個「根」。

+0

謝謝,我添加了其他兩個變量,程序現在可以運行。但是'csv_name_sub'和'csv_name_ses'仍然不會傳遞給第二個類,因爲這些值不能在第二個類中打印。你知道這個傳遞變量問題的錯誤是什麼?謝謝! – Kay

0

由於Welcome類的構造函數爲__init__(self, master, csv_name_sub, csv_name_ses),因此您需要提供csv_name_subcsv_name_ses的值以及實例化此類。既然你不實際使用的csv_name_subcsv_name_ses傳遞的值(見第4行的Welcome類的__init__功能),這可能是一個可行的解決方案:

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 
+0

感謝您的回答。但它不會傳遞'class App'中的變量。我並不真正瞭解你的意思,因爲我只是在命名我的文檔時使用這些值,所以實際上並沒有使用這些值。這與其他類型的用途有什麼不同? – Kay

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()) 
+0

感謝您的建議。我嘗試過使用'csv_name_sub = None','csv_name_ses = None',但變量不能從'class Welcome'傳遞給'class App'。你有更詳細的解釋,爲什麼我應該在這裏添加'None'?謝謝! – Kay

相關問題