Python 'NoneType' 对象没有属性 'config'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23231563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'NoneType' object has no attribute 'config'
提问by Kyle
What I am trying to do here is add the image to the button I have, then based on click or hover change the image. All the examples I have followed use the .config()method.
我在这里尝试做的是将图像添加到我拥有的按钮,然后基于单击或悬停更改图像。我遵循的所有示例都使用该.config()方法。
For the life of me I can't figure out why it doesn't know what the button object is. What is interesting is that if I modify the Button definition line to include the image option, everything is fine. But, with that there it seems I can't modify it using .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)
采纳答案by Matteo Italia
pButton = Button(root, text="Play", command="playButton").grid(row=1)
Here you are creating an object of type Button, but you are immediately calling the gridmethod over it, which returns None. Thus, pButtongets assigned None, and that's why the next row fails.
在这里,您正在创建一个类型为 的对象Button,但您立即对其调用grid方法,该方法返回None. 因此,pButton会被分配None,这就是下一行失败的原因。
You should do instead:
你应该这样做:
pButton = Button(root, text="Play", command="playButton")
pButton.grid(row=1)
pButton.config(image=PlayUp)
i.e. first you create the button and assign it to pButton, thenyou do stuff over it.
即首先创建按钮并将其分配给pButton,然后对其进行处理。

