2015-12-27 34 views
0

對於此代碼,我的意圖是添加一個函數以添加用戶輸入到數據庫中的數據。我已經創建了一個函數來創建用戶界面,以便讓他們輸入數據和另一個函數來檢索數據。但是,我收到一條錯誤消息,指出DeliveryInterface變量尚未定義。有沒有辦法讓add data函數識別最後一個函數的DeliveryInterface變量?我附上導致問題的代碼段。調用tkinter窗口變量,以便可以在另一個函數中使用

 def InputScreen(): 
      DeliveryInterface = tkinter.Tk() #creates the interface for the input screen 
      DeliveryInterface.title('Input New Data') #gives the new window a title 
      DeliveryInterface.geometry('400x500') #sets the default size of the window as 400 x 500 
      DeliveryInterface.maxsize(400, 500) #sets the maximum size of the interface as 400 x 500 
      DeliveryInterface.configure(background='#0092FF') #sets the background colour of the inteface to the light blue as seen in the main menu interface 
      (.......) 
      StockIDLabel = tkinter.Label(DeliveryInterface, text='Stock ID', font=('Helvetica', 16)) # creates a label that tells the user that the next box is associated with the stock id 
      StockIDLabel.place(x=30, y=60, height=22, width=200) 

      StockID_entry = tkinter.Entry(DeliveryInterface, font=('Helvetica', 16)) # creates the entry box for the stock ID 
      StockID_entry.place(x=250, y=60, height=22, width=100) 
      (...........) 
     def addingNewStock(): 
      StockID = 0 #Initialises the stock ID 
      StockID = DeliveryInterface.StockID_entry.get() #intended to get the data from the entrybox, however, i get an error message say that DeliveryInterface hasnt been defined 
      print (StockID) #prints the stockID so that i now if the variable has been changed. 

我希望這是需要的所有信息,謝謝

+0

在一個函數中定義的變量對於定義函數是局部的。另外,你的縮進也會搞砸了。 – ppperry

回答

0

你必須要DeliveryInterface全球:

def InputScreen(): 
     global DeliveryInterface 
     DeliveryInterface = tkinter.Tk() #creates the interface for the input screen 
     DeliveryInterface.title('Input New Data') #gives the new window a title 
     DeliveryInterface.geometry('400x500') #sets the default size of the window as 400 x 500 
     DeliveryInterface.maxsize(400, 500) #sets the maximum size of the interface as 400 x 500 
     DeliveryInterface.configure(background='#0092FF') #sets the background colour of the inteface to the light blue as seen in the main menu interface 
     (.......) 
     StockIDLabel = tkinter.Label(DeliveryInterface, text='Stock ID', font=('Helvetica', 16)) # creates a label that tells the user that the next box is associated with the stock id 
     StockIDLabel.place(x=30, y=60, height=22, width=200) 

     StockID_entry = tkinter.Entry(DeliveryInterface, font=('Helvetica', 16)) # creates the entry box for the stock ID 
     StockID_entry.place(x=250, y=60, height=22, width=100) 
     (...........) 
    def addingNewStock(): 
     StockID = 0 #Initialises the stock ID 
     StockID = DeliveryInterface.StockID_entry.get() #intended to get the data from the entrybox, however, i get an error message say that DeliveryInterface hasnt been defined 
     print (StockID) #prints the stockID so that i now if the variable has been changed. 

另一種選擇是使用Python 3的nonlocal關鍵字。

2

你面臨的問題是,變量的作用域 - 您所定義DeliveryInterface在InputScreen()存在 - 它不會存在於其他地方。

您有幾種選擇如何解決這個問題:

使它成爲一個全局變量。這通常被認爲是不好的做法,但仍然是合理的。

使InputScreen成爲一個類,該類具有用於初始化和添加新股票的方法。通過這樣做,課程會記住它的狀態 - 變量的值。

或者將DeliveryInterface作爲參數傳遞給addingNewStock()函數。如果你從函數InputScreen()調用這個函數,那麼這將是最簡單的解決方案。

相關問題