【问题标题】:Tkinter Entry to store into a variable returns nothing存储到变量中的 Tkinter 条目不返回任何内容
【发布时间】:2020-05-16 20:17:40
【问题描述】:

好的,所以我一直在尝试从 Tkinter 的条目中获取用户输入并将其存储到在程序末尾打印的变量中,但是当打印变量时,输出是初始值。

代码:

from tkinter import *
hello = ''
root = Tk()

e = Entry(root, width = 50)
e.pack()
def myClick():
    hello = e.get()
button1 = Button(root, text = 'ik', command = myClick)
button1.pack()
root.mainloop()
print(hello)

【问题讨论】:

标签: python tkinter tkinter-entry


【解决方案1】:

myClick 中的hello 变量是一个局部变量。要让它引用全局变量,您需要将其声明为全局变量。

def myClick():
    global hello
    hello = e.get()

【讨论】:

  • 非常感谢!没想到。
猜你喜欢
  • 1970-01-01
  • 2018-12-14
  • 1970-01-01
  • 2011-04-26
  • 1970-01-01
  • 2021-04-01
  • 1970-01-01
相关资源
最近更新 更多