Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 条目配置错误_Python_Widget_Tkinter_Tk - Fatal编程技术网

Python 条目配置错误

Python 条目配置错误,python,widget,tkinter,tk,Python,Widget,Tkinter,Tk,我有一个程序需要更改小部件的状态,但问题是我不断收到此错误: 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 3

我有一个程序需要更改小部件的状态,但问题是我不断收到此错误:

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

提前谢谢

pack
不返回任何内容。所以现在的条目是
None
。此外,在
tk
中使用
time.sleep
不是一个好选择。您可以使用:

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

pack
不返回任何内容。所以现在的条目是
None
。此外,在
tk
中使用
time.sleep
不是一个好选择。您可以使用:

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

您的问题是
Entry
pack
方法实际上返回的是
None
,而不是entrybox。您可以将
打印(条目)
放在定义
条目的行的正下方,从而看到这一点。它将在终端中打印
None
。要解决此问题,请将
pack
放在它自己的行上,如下所示:

entry = Entry(root, fg='blue', textvariable=check_var)
entry.pack(side=TOP)
现在,
entry
像它应该的那样引用entrybox,而不是entrybox的
pack
方法

另外,如果您计划在脚本中重复使用
change
,那么将其放入命名函数中是一件好事。否则,我只会这样做:

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

它更简洁,使用
lambda
意味着不需要定义一个只使用一次的完整命名函数。

您的问题是
Entry
pack
方法实际上返回
None
,而不是entrybox。您可以将
打印(条目)
放在定义
条目的行的正下方,从而看到这一点。它将在终端中打印
None
。要解决此问题,请将
pack
放在它自己的行上,如下所示:

entry = Entry(root, fg='blue', textvariable=check_var)
entry.pack(side=TOP)
现在,
entry
像它应该的那样引用entrybox,而不是entrybox的
pack
方法

另外,如果您计划在脚本中重复使用
change
,那么将其放入命名函数中是一件好事。否则,我只会这样做:

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

它更简洁,使用
lambda
意味着不需要只为一次使用而定义一个完整的命名函数。

虽然您所说的是正确的,代码是有效的,但您没有解决真正的问题(提示:这与lambda的使用无关)@BryanOakley:谢谢您的提示。让我添加一个101。虽然你说的是真的,代码也可以运行,但你没有解决真正的问题(提示:这与lambda的使用无关)@BryanOakley:谢谢你的提示。让我加上一个101。