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

Kyl*_*yle 7 python image button nonetype

我在这里尝试做的是将图像添加到我拥有的按钮,然后基于点击或悬停更改图像.我所遵循的所有示例都使用该.config()方法.

对于我的生活,我无法弄清楚为什么它不知道按钮对象是什么.有趣的是,如果我修改Button定义行以包含图像选项,一切都很好.但是,有了它,似乎我不能使用它来修改它.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)
Run Code Online (Sandbox Code Playgroud)

Mat*_*lia 22

pButton = Button(root, text="Play", command="playButton").grid(row=1)
Run Code Online (Sandbox Code Playgroud)

在这里你要创建一个类型的对象Button,但是你立即调用grid它的方法,它返回None.因此,pButton被分配None,这就是下一行失败的原因.

你应该这样做:

pButton = Button(root, text="Play", command="playButton")
pButton.grid(row=1)
pButton.config(image=PlayUp)
Run Code Online (Sandbox Code Playgroud)

即首先你创建按钮并分配给它pButton,然后你做它的东西.

  • 谢谢.我花了很长时间想知道为什么这不起作用哈哈! (2认同)