调用tkinter窗口变量,以便可以在另一个函数中使用它

时间:2015-12-27 00:05:04

标签: python python-3.x tkinter

对于此代码,我的目的是添加一个函数来添加用户输入数据库的数据。我已经设置了一个函数来创建用户界面,以便让他们输入数据和另一个函数来检索数据。但是,我收到一条错误消息,指出尚未定义DeliveryInterface变量。有没有办法让add数据函数识别上一个函数的DeliveryInterface变量?我附上导致问题的代码段。

        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.

我希望这是所需的全部信息,谢谢

2 个答案:

答案 0 :(得分:2)

您面临的问题是变量具有其范围 - 您已将InputInterface定义为存在于InputScreen()中 - 并且它不会存在于其他任何位置。

您可以选择如何解决此问题:

使其成为全局变量。这通常是不赞成的,被认为是一种不好的做法,但仍然是合理的。

使InputScreen成为一个具有初始化方法和添加新股票的类。通过这样做,类将记住它的状态 - 变量的值。

或者将DeliveryInterface作为参数传递给addsNewStock()函数。如果您正在使用InputScreen()函数调用此函数,那么这将是最简单的解决方案。

答案 1 :(得分: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关键字。

相关问题