[英]How to set focus for Tkinter widget?
我有一個簡單的Python + Tkinter應用程序,它顯示了10個項目的列表:
import Tkinter, ttk
list = ttk.Treeview( Tkinter.Tk() )
list.pack( fill = Tkinter.BOTH, expand = 1 )
items = [ list.insert( '', 'end', text = str( i ) ) for i in range( 10 ) ]
list.selection_set( items[ 0 ] )
list.focus_set() # This is not working - list has no focus :(
Tkinter.mainloop()
是否可以修改它,以便在應用程序啟動后,列表將具有焦點,我可以通過向上和向下箭頭移動選擇? 應用程序啟動后,應用程序的窗口有焦點,但我無法用箭頭移動選擇,直到我用鼠標單擊列表:(。我嘗試了focus_set()
和focus_force()
不同組合,但它不起作用。
在Windows 7,OSX 10.7和Ubuntu 12.04上使用Python 2.7進行檢查
UPDATE
如果將“Treeview”更改為某個其他窗口小部件,例如更改為“按鈕”,則焦點正在工作。 所以我似乎錯誤地為Treeview設置了焦點。
最后找到了一個解決方案 - 似乎Treeview
小部件需要設置焦點兩次:首先是小部件本身,第二個是項目:
list.selection_set( items[ 0 ] )
list.focus_set()
list.focus( items[ 0 ] ) # this fixes a problem.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.