Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'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)
like image 405
Kyle Avatar asked Apr 22 '14 22:04

Kyle


3 Answers

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.

like image 106
Matteo Italia Avatar answered Oct 15 '22 23:10

Matteo Italia


I do not know if it helps in similar cases:

mywidget = Tkinter.Entry(root,textvariable=myvar,width=10).pack()
mywidget.config(bg='red')

This generates this error:

AttributeError: 'NoneType' object has no attribute 'config'

But everythings run smoothly if I write on separate lines:

mywidget = Tkinter.Entry(root,textvariable=myvar,width=10)
mywidget.pack()
mywidget.config(bg='red')

I do not understand it but it took me a lot of time to solve... :-(

like image 41
xerostomus Avatar answered Oct 15 '22 23:10

xerostomus


canvas = tk.Tk()
canvas.title("Text Entry")

text_label = ttk.Label(canvas, text = "Default Text")
text_label_1 = ttk.Label(canvas, text = "Enter Your Name: ").grid(column = 0, row = 1)
text_variable = tk.StringVar()
text_entry = ttk.Entry(canvas, width = 15, textvariable = text_variable)
text_entry.grid(column = 1, row = 1)
def change_greeting():
    text_label.configure(text = "Hello " + text_variable.get())
event_button = ttk.Button(canvas, text="Click me and see what happens", command = change_greeting).grid(column = 2, row = 1)
text_label.grid(column = 0, row = 0)
canvas.resizable(False, False)
canvas.mainloop()

In the above code text_label.grid(column = 0, row = 0) is placed at the end that is after all the operations on the label is completed.
If text_label = ttk.Label(canvas, text = "Default Text").grid(column = 0, row = 0) is done in the above code then the error will be produced. Hence being cautious about the display of objects is necessary

like image 1
Narasim Y Avatar answered Oct 15 '22 22:10

Narasim Y