對於此代碼,我的意圖是添加一個函數以添加用戶輸入到數據庫中的數據。我已經創建了一個函數來創建用戶界面,以便讓他們輸入數據和另一個函數來檢索數據。但是,我收到一條錯誤消息,指出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.
我希望這是需要的所有信息,謝謝
在一個函數中定義的變量對於定義函數是局部的。另外,你的縮進也會搞砸了。 – ppperry