【问题标题】:Properly print entry widget input Python3.5正确打印入口小部件输入Python3.5
【发布时间】:2016-01-21 02:54:00
【问题描述】:

好的,这是我的问题:

下面的代码在被按钮调用时会打印出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 打印到控制台。这是因为它在一个单独的窗口中吗?如果是这样,我该如何解决?

【问题讨论】:

    标签: python python-3.x tkinter python-3.5


    【解决方案1】:

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

    代替:

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

    像这样定义 Entry 小部件:

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

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

    print(userName.get())
    

    适用于您想要的所有 Entry 小部件。

    【讨论】:

      猜你喜欢
      • 2019-02-22
      • 2013-04-03
      • 1970-01-01
      • 2017-08-25
      • 1970-01-01
      • 2021-02-16
      • 2021-02-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多