【问题标题】:Difficulty updating a label inside a button callback难以在按钮回调中更新标签
【发布时间】:2013-03-20 16:05:11
【问题描述】:

我是 Tkinter 和 Python 3.3 的新手,并试图开发一个简单的 GUI。我有一个标签“statusLabel”。当我单击按钮时,我想更新按钮回调中标签中的值,但出现错误。

line 12, in reportCallback
    statusLabel.config(text="Thank you. Generating report...")
AttributeError: 'NoneType' object has no attribute 'config'

下面是我的代码

from tkinter import *
root = Tk()

Label(root,text="Project folders. Include full paths. One project per line").pack()
Text(root,height=4).pack()
Label(root,text="Standard project subfolders. Include path from project.").pack()
Text(root,height=4).pack()

statusLabel = Label(root,text="Oh, hello.").pack()

def reportCallback():
    statusLabel.config(text="Thank you. Generating report...")

b = Button(root, text="Generate Report", command=reportCallback).pack()

root.mainloop()

【问题讨论】:

  • 如果你想在一个定义的函数中使用你的 statusLabel (它没有在那里定义 -> NoneType),你是否应该让你的 statusLabel 全局化

标签: python callback tkinter label config


【解决方案1】:

这行是问题所在:

statusLabel = Label(root,text="Oh, hello.").pack()

.pack() 返回None。大概,您希望statusLabel 持有对您刚刚创建的Label 对象的引用。

试试这个:

statusLabel = Label(root,text="Oh, hello.")
statusLabel.pack()

例如,请参阅此处第一个清单中的琐碎程序:http://effbot.org/tkinterbook/label.htm

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多