'NoneType' 对象没有属性 'config' [英] 'NoneType' object has no attribute 'config'
问题描述
我在这里要做的是将图像添加到我拥有的按钮上,然后根据单击或悬停更改图像.我遵循的所有示例都使用 .config()
方法.
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.
在我的一生中,我无法弄清楚为什么它不知道按钮对象是什么.有趣的是,如果我修改 Button 定义行以包含图像选项,一切都很好.但是,在那里似乎我无法使用 .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()
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)
推荐答案
pButton = Button(root, text="Play", command="playButton").grid(row=1)
在这里,您正在创建一个 Button
类型的对象,但您立即对其调用了 grid
方法,该方法返回 None
.因此,pButton
被分配了 None
,这就是下一行失败的原因.
Here you are creating an object of type Button
, but you are immediately calling the grid
method over it, which returns None
. Thus, pButton
gets assigned None
, and that's why the next row fails.
你应该这样做:
pButton = Button(root, text="Play", command="playButton")
pButton.grid(row=1)
pButton.config(image=PlayUp)
即首先,您创建按钮并将其分配给 pButton
,然后您可以对其进行处理.
i.e. first you create the button and assign it to pButton
, then you do stuff over it.
这篇关于'NoneType' 对象没有属性 'config'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!