正确打印条目小部件输入 Python3.5

Properly print entry widget input Python3.5

好的,这是我的问题:

下面的代码,当被按钮调用时,打印 NOTHING。为什么? (是的,我当然是在输入框中输入内容。)

options = Tk()
options.geometry("218x156")
options.iconbitmap('original.ico')
options.title("Options")

Label(options, text = "GitHub Username:").pack()
userName = StringVar()
Entry(options, width = "30", textvariable = userName).pack()

Label(options, text = "GitHub Email:").pack()
userEmail = StringVar()
Entry(options, width = "30", textvariable = userEmail).pack()

Label(options, text = "GitHub Password:").pack()
userPassword = StringVar()
Entry(options, show = "•", width = "30", textvariable = userPassword).pack()

def optionSave():
    print(userName.get())
    print(userEmail.get())
    print(userPassword.get())
    options.destroy()

有效,并且

def fileOptions():
    options = Tk()
    options.geometry("218x156")
    options.iconbitmap('original.ico')
    options.title("Options")

    Label(options, text = "GitHub Username:").pack()
    userName = StringVar()
    Entry(options, width = "30", textvariable = userName).pack()

    Label(options, text = "GitHub Email:").pack()
    userEmail = StringVar()
    Entry(options, width = "30", textvariable = userEmail).pack()

    Label(options, text = "GitHub Password:").pack()
    userPassword = StringVar()
    Entry(options, show = "•", width = "30", textvariable = userPassword).pack()

    def optionSave():
        print(userName.get())
        print(userEmail.get())
        print(userPassword.get())
        options.destroy()

没有

请注意:唯一明显的区别是

上的代码

底部没有包起来def fileOptions():

输出为空白。正如它在控制台中打印 zilch 一样。这是因为它在单独的 Window 中吗?如果是这样,我该如何解决?

现在...回答我自己的问题!

而不是:

userName = StringVar()
Entry(options, width = "30", textvariable = userName).pack()

像这样定义条目小部件:

userName = StringVar()
userName = Entry(options, width = "30")
userName.pack()

.pack() 向下移动到它自己的行,如 userName.pack() 然后像这样打印它:

print(userName.get())

您想要的所有条目小部件。