'NoneType'对象有没有属性'配置' [英] 'NoneType' object has no attribute 'config'

查看:933
本文介绍了'NoneType'对象有没有属性'配置'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么,我想在这里做的是图像添加到按钮我有,然后根据点击或悬停改变形象。我按照所有的例子使用的.config方法。对于我的生活中,我想不通为什么它不知道该按钮对象是什么。有趣的是,如果我修改按钮定义线,包括图像选项,一切都很好。但是,随着有我似乎无法使用的.config修改

  PlayUp =光象(文件= currentdir +'\\ Up_image.gif')
PlayDown =光象(文件= currentdir +'\\ Down_image.gif')
#功能
DEF为playButton():
    pButton.config(图像= PlayDown)
pButton =按钮(根,文本=播放命令=为playButton)。格(行= 1)
pButton.config(图像= PlayUp)


解决方案

  pButton =按钮(根,文本=播放命令=为playButton)。格(行= 1 )

在这里,您正在创建类型的对象按钮,但你会立即调用格在它方法,返回。因此, pButton 被分配,这就是为什么下一行失败。

您应该做的,而不是:

  pButton =按钮(根,文本=播放命令=为playButton)
pButton.grid(行= 1)
pButton.config(图像= PlayUp)

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

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. 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)

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.

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, then you do stuff over it.

这篇关于'NoneType'对象有没有属性'配置'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆