Reputation: 2072
I have a program that needs to change the state of the widget but the problem is I keep getting this error:
Traceback (most recent call last):
File "C:\Users\Public\Documents\Programming\Spellox\Spellox.py", line 67, in <module>
new_word()
File "C:\Users\Public\Documents\Programming\Spellox\Spellox.py", line 37, in new_word
entry['state'] = DISABLED
TypeError: 'NoneType' object does not support item assignment
Can anyone help here is my code for the entry widget:
from tkinter import *
import time
root = Tk()
entry = Entry(root, fg='blue', textvariable=check_var).pack(side=TOP)
def change():
time.sleep(3)
entry['state'] = DISABLED
change()
root.mainloop()
Thanks In Advance!
Upvotes: 1
Views: 411
Reputation:
Your problem is that the pack
method of Entry
actually returns None
, not the entrybox. You can see this by putting print(entry)
right beneath the line where you define entry
. It will print None
in the terminal. To fix this problem, put pack
on it's own line like this:
entry = Entry(root, fg='blue', textvariable=check_var)
entry.pack(side=TOP)
Now, entry
refers to the entrybox like it should, not the pack
method of the entrybox.
Also, if you plan to use change
repeatedly in your script, then putting it in a named function is a good thing. Otherwise, I would simply do this:
from tkinter import *
root = Tk()
check_var = StringVar()
entry = Entry(root, fg='blue', textvariable=check_var)
entry.pack(side=TOP)
entry.after(3000, lambda:entry.config(state=DISABLED))
root.mainloop()
It's more concise and using a lambda
means there is no need to define a whole named function just to be used one time.
Upvotes: 2
Reputation: 8610
pack
returns nothing. So entry now is a None
. Besides, using time.sleep
in tk
is not a good choice. You may use:
from tkinter import *
root = Tk()
check_var = StringVar()
entry = Entry(root, fg='blue', textvariable=check_var)
def change():
entry['state'] = DISABLED
entry.after(3000, change)
entry.pack(side=TOP)
root.mainloop()
Upvotes: 2