当我调用时,为什么没有返回.get()

时间:2016-10-26 22:32:25

标签: python tkinter get set

无法设置用户选择的文件路径并设置为变量。我能够检索路径并将其设置为在输入框中显示,但我想捕获该路径并将其导入另一个脚本。也许我的逻辑在这里有缺陷?我做错了什么?

    import Tkinter
    import tkFileDialog
    from Tkinter import *
    from tkFileDialog import *

class GUI:
    def __init__(self, master):
        self.master = master



        master.title("XML Compare Tool")
        master.geometry('700x300')

        path1 = StringVar()
        path2 = StringVar()
        self.bb1 = Button(master, text="Browse", command=lambda: path1.set(askopenfilename()))
        self.bb1.grid(row=0, column=0, padx=5, pady=5)


        self.bb2 = Button(master, text="Browse", command=lambda: path2.set(askopenfilename()))
        self.bb2.grid(row=1, column=0, padx=5, pady=5)

        self.confirm = Button(master, text="Confirm", command='')
        self.confirm.grid(row=3, column=1, padx=5, pady=5, sticky='')

        self.entry1 = Entry(master, width=75, textvariable=path1)
        self.entry1.grid(row=0, column=1, columnspan=2, sticky=W)
        print path1.get()

        self.entry2 = Entry(master, width=75, textvariable=path2)
        self.entry2.grid(row=1, column=1, sticky=W)

        self.t_label = Label(master, text="Script Output")
        self.t_label.grid(row=4, column=1, columnspan=1, sticky='')

        self.t_frame = Frame(master, bg="white", height=150, width=600)
        self.t_frame.grid(row=5, column=1, columnspan=1, sticky='')
        self.t_text = Text(self.t_frame)



root = Tk()
my_gui = GUI(root)
root.mainloop()

1 个答案:

答案 0 :(得分:1)

您不需要使用textvariable,只需使用variable = entry1.get()即可。 Tkinter textvariable与传统的python变量不同,它只用于在条目中设置文本。