亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法在條目小部件上設置 focus()

無法在條目小部件上設置 focus()

慕容708150 2021-12-17 10:31:34
當我嘗試將焦點設置在條目小部件上時,我收到錯誤消息,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'
查看完整描述

1 回答

?
米脂

TA貢獻1836條經驗 獲得超3個贊

您正在尋找的功能focus_set()focus(). 關于該方法的簡要文檔focus_set()。

編輯:

此外,該線路entSearch = entSearch.grid(row = 0, column = 1, sticky=W)也引起了問題。默認情況下,該grid方法不返回值(僅顯示給定的小部件)。因此,它返回None——你存儲這個值entSearch意味著你不能“聚焦”在一個None對象上。要解決這個問題,只需刪除分配并顯示您的小部件:entSearch.grid(row = 0, column = 1, sticky=W)


查看完整回答
反對 回復 2021-12-17
  • 1 回答
  • 0 關注
  • 324 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號