【问题标题】:Widget is equal to None [duplicate]小部件等于无 [重复]
【发布时间】:2015-02-16 01:06:18
【问题描述】:

我正在使用 Tkinter 制作一个 GUI,它允许在其中一个窗口中输入数据:

enterscore = Entry(window4, font="Helvatica 25", textvariable = newscore).pack()

我正在尝试制作一个按钮,它将输入的数据保存为变量:

savebttn = Button(window4, text= "Save", width=5, height=2, font="Helvatica 25", command = savescore).pack()

地点:

def savescore():
    score = enterscore.get()

但是,如果我运行程序,它会返回错误消息:

AttributeError: 'NoneType' object has no attribute 'get'

我哪里错了?

【问题讨论】:

  • 对不起,我没有意识到这是重复的,因为我不明白问题出在哪里!

标签: python python-3.x tkinter nonetype


【解决方案1】:

pack 方法返回None。您需要将创建 Entry 的语句和打包的语句分开。

enterscore = Entry(window4, font="Helvatica 25", textvariable=newscore)
enterscore.pack()

savebttn 也是如此。

【讨论】:

    【解决方案2】:

    不要这样做:

    enterscore = Entry(window4, font="Helvatica 25", textvariable = newscore).pack()
    

    调用pack 返回None,而不是小部件。 tkinter 不支持这种方法链接。而是这样做:

    enterscore = Entry(window4, font="Helvatica 25", textvariable = newscore)
    enterscore.pack()
    

    这会构造小部件,并设置enterscore 指向它。然后调用enterscore.pack 与上面的效果相同,但eneterscore 指向正确的对象(小部件,而不是None

    【讨论】:

      猜你喜欢
      • 2014-11-01
      • 2014-05-05
      • 2020-02-20
      • 2012-02-14
      • 1970-01-01
      • 2021-02-02
      • 2013-08-30
      • 2014-06-08
      • 2021-06-11
      相关资源
      最近更新 更多