当我尝试将重点放在条目小部件上时,我收到一条错误消息
Traceback (most recent call last):
File "C:/PythonPrograms/Tkinter/test_case.py", line 13, in <module>
entSearch.focus()
AttributeError: 'NoneType' object has no attribute 'focus'
通过在堆栈溢出中搜索此错误的其他出现,此修复程序似乎是在单独的一行上调用grid方法。
entSearch = Entry(main, textvariable = text, width = 50, font='arial 12')
entSearch = entSearch.grid(row = 0, column = 1, sticky=W)
而不是
entSearch = Entry(main,
textvariable = text,
width = 50,
font='arial 12').grid(row = 0, column = 1, sticky=W)
很遗憾,此修复程序不适用于我。
from tkinter import *
main = Tk()
main.title("Test Case")
main.geometry('750x750')
main.configure(background='ivory3')
text = StringVar()
entSearch = Entry(main, textvariable = text, width = 50, font='arial 12')
entSearch = entSearch.grid(row = 0, column = 1, sticky=W)
entSearch.focus()
main.mainloop()
预期,在运行代码时,Entry Widget将成为焦点。
但是我收到错误消息
Traceback (most recent call last):
File "C:/PythonPrograms/Tkinter/test_case.py", line 13, in <module>
entSearch.focus()
AttributeError: 'NoneType' object has no attribute 'focus'
答案 0 :(得分:2)
您要查找的功能是focus_set()
,而不是focus()
。关于focus_set()
方法的简短documentation。
编辑:
此外,行entSearch = entSearch.grid(row = 0, column = 1, sticky=W)
也引起了问题。默认情况下,grid
方法不返回值(仅显示给定的窗口小部件)。因此,它返回None
-您将此值存储在entSearch
中,这意味着您不能“关注” None
对象。要解决此问题,只需删除任务并显示您的窗口小部件,如下所示:entSearch.grid(row = 0, column = 1, sticky=W)