[Solved] TypeError: ‘NoneType’ object does not support item assignment?

So I start a root screen with a “file select” and a “go” button. The go button is disabled and I want to make it active after the file has been selected. When I select the file go should become active but instead this error “TypeError: ‘NoneType’ object does not support item assignment” Here is some sample code

import Tkinter
import tkFileDialog

def chooseDir():
    dir1=tkFileDialog.askopenfilename(parent=root, title='choose file path')
    go['state']=Tkinter.ACTIVE
root=Tkinter.Tk()
global go
go=Tkinter.Button(text='file location',command=chooseDir,state=Tkinter.DISABLED).pack()
root.mainloop()
Enquirer: Kosig

||

Solution #1:

This line:

go=Tkinter.Button(text='file location',command=chooseDir,state=Tkinter.DISABLED).pack()

is creating a temporary object, then calling pack() on it. The pack method returns None, so go is assigned None.

Remove the .pack() then go will be the Button object. Then call go.pack().

Respondent: Kosig

Solution #2:

(update for new error)
go doesn’t exist in that scope, you’ll need to get access to it somehow, or by using the one in the global scope using global go inside the chooseDir method perhaps

Tkinter.Buttons don’t behave like dictionaries, you can change their status via the config() method.

Try:

go.config(state=Tkinter.ACTIVE)
Respondent: Keith

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.