'NoneType'对象没有属性'config'

发布于 2021-01-29 16:53:10

我在这里想要做的就是将图像添加到我拥有的按钮中,然后基于单击或悬停更改图像。我遵循的所有示例均使用该.config()方法。

对于我的一生,我无法弄清楚为什么它不知道按钮对象是什么。有趣的是,如果我修改Button定义行以包括image选项,一切都很好。但是,有了它,似乎我无法使用.config()

PlayUp = PhotoImage(file=currentdir+'\Up_image.gif')
PlayDown = PhotoImage(file=currentdir+'\Down_image.gif')
#Functions
def playButton():
    pButton.config(image=PlayDown)
pButton = Button(root, text="Play", command="playButton").grid(row=1)
pButton.config(image=PlayUp)
关注者
0
被浏览
51
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。
    pButton = Button(root, text="Play", command="playButton").grid(row=1)
    

    在这里,您正在创建类型的对象Button,但是您立即对其进行了调用,该grid方法返回None。因此,pButton被分配了None,这就是下一行失败的原因。

    您应该改为:

    pButton = Button(root, text="Play", command="playButton")
    pButton.grid(row=1)
    pButton.config(image=PlayUp)
    

    即,首先创建按钮并将其分配给pButton然后 对它进行填充。



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看