tkinter-更改分配给标签的变量会导致窗口重复

时间:2018-07-04 23:01:06

标签: python user-interface variables tkinter

我正在使用python与一些excel电子表格进行交互。我已经完成了所有工作,现在我正在使用tkinter在UI上工作。我有3个按钮,一个用于拉出数据文件的位置,输出文件的保存位置,还有一个开始按钮。

我正在尝试使用tkinter.Label来显示前两个按钮的值,例如"c:/user/data_file"。但是,每当我从用户那里获取变量并尝试使用它更新GUI时,就会使用更新后的信息来创建窗口的副本。我需要它无缝地直接直接更新到当前窗口。我一直在努力解决这个问题,但是我无法弄清楚。以下是我的tkinter东西的代码。

def main(): 

    def InputFilePrompt():
        global InputFileLocation
        InputFileLocation = askopenfilename()
        update()  

    def OutputFilePrompt():
        global OutputFileLocation
        OutputFileLocation = filedialog.asksaveasfilename()
        update()

    def update():

        root = Tk()

        root.title("test")
        root.resizable(width=TRUE,height=TRUE)

        InputFile = Button(root, text = "input data", command = InputFilePrompt)
        InputFile.grid(row = 0,column = 0)

        InputFileValue = Label(root, text = InputFileLocation, bg = 'white')
        InputFileValue.grid(row = 1,column = 0)

        OutputFile = Button(root, text = "Compiled Data save loacation", command = OutputFilePrompt)
        OutputFile.grid(row = 4,column = 0)

        OutputFileValue = Label(root, text = "location: N/A", bg = 'white')
        OutputFileValue.grid(row = 5,column = 0)

        startButton = Button(root, text = "start", bg = 'light green', command = Excel)
        startButton.grid(row = 7)

        BlankUI = [0 for x in range(2)]
        for blankspace in range(2):
            BlankUI[blankspace] = Label(root, text = "")
        BlankUI[0].grid(row = 2)
        BlankUI[1].grid(row = 6)

        root.mainloop()   

    update()

错误:

Picture of the Error

1 个答案:

答案 0 :(得分:0)

这是一个不会创建重复窗口的版本。我已经在注释中提出了大多数建议,除了关于在其他函数中定义函数的建议之外。以下内容仍会这样做,因为这样做很容易避免使用global变量(通常认为这是不良的编程习惯)。

请注意,没有update()函数。现在,两个tkinter.Label的值存储在两个tkinter.StringVar对象中,而不是常规的Python字符串中。 StringVartkinter所谓的“变量”类之一。它们的主要功能是,只要更改其内容,所有引用它们的小部件都将自动更新。要在Label中使用它们,请在调用构造函数时使用textvariable=选项(而不是text= )来指定它们。 这是我发现的一些documentation,有关它们的工作原理的详细信息。

import tkinter as tk
from tkinter.filedialog import askopenfilename, asksaveasfilename

def excel():
    """ Undefined. """
    pass


def main():
    def get_input_file_location():
        input_file_location.set(askopenfilename())

    def get_output_file_location():
        output_file_location.set(asksaveasfilename(confirmoverwrite=False))

    root = tk.Tk()
    root.title('test')
    root.resizable(width=True, height=True)

    input_file_location = tk.StringVar()
    input_file_location.set('<undefined>')
    output_file_location = tk.StringVar()
    output_file_location.set('<undefined>')

    input_file = tk.Button(root, text="Input data",
                           command=get_input_file_location)
    input_file.grid(row=0, column=0)
    input_file_value = tk.Label(root, textvariable=input_file_location,
                                bg='white')
    input_file_value.grid(row=1, column=0)

    output_file = tk.Button(root, text='Compiled data save loacation',
                            command=get_output_file_location)
    output_file.grid(row=4, column=0)
    output_file_value = tk.Label(root, textvariable=output_file_location,
                                 bg='white')
    output_file_value.grid(row=5, column=0)

    startButton = tk.Button(root, text='start', bg='light green',
                            command=excel)
    startButton.grid(row=7)

    blank_ui = [tk.Label(root, text='') for _ in range(2)]
    blank_ui[0].grid(row=2)
    blank_ui[1].grid(row=6)

    root.mainloop()


if __name__ == '__main__':
    main()