【问题标题】:Tkinter Entry .get from a definition [duplicate]Tkinter Entry .get 从定义 [重复]
【发布时间】:2020-06-30 14:40:58
【问题描述】:

我不断收到一个无类型属性错误,并且在查看许多视频时不知道如何修复它,但没有任何帮助。我不知道我做错了什么。任何帮助表示赞赏!

from tkinter import *

def find():
    user = whiteBox.get()
    print(user)
root = Tk()
weatherResult = StringVar()
weatherResult.set("Enter a Place")

weather = Label(root, textvariable=weatherResult).pack()
whiteBox = Entry(root).pack()
check = Button(root, text="find", command=find).pack()
root.mainloop()

【问题讨论】:

  • 这是否回答了您的问题? stackoverflow.com/questions/1101750/…
  • 如果对您有帮助,请您接受我的回答吗?如果它比我的更好,您可以稍后接受另一个答案。
  • @10Rep 这似乎是唯一正确的答案:D
  • @Atlas435 是的 :)。但我只是说,以防万一有更多详细信息出现。

标签: python python-3.x tkinter tk


【解决方案1】:

你犯了一个非常常见的错误。您的小部件的值将是 NoneType,因为您在同一行上使用了 .pack

所以,你的代码:

from tkinter import *

def find():
    user = whiteBox.get()
    print(user)
root = Tk()
weatherResult = StringVar()
weatherResult.set("Enter a Place")

weather = Label(root, textvariable=weatherResult).pack()
whiteBox = Entry(root)
whiteBox.pack()
check = Button(root, text="find", command=find).pack()
root.mainloop()

这应该是您想要的结果。希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2022-12-02
    • 2021-02-07
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    • 2019-06-23
    • 2019-02-26
    • 2015-09-29
    • 1970-01-01
    相关资源
    最近更新 更多