Kyle Kyle - 5 days ago 5
Python Question

'NoneType' object has no attribute '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. 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)

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

Comments