9

我有一个简单的 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()

是否可以修改它,以便在应用程序启动后,列表将具有焦点,我可以通过向上和向下箭头移动选择?应用程序启动后,应用程序的窗口具有焦点,但我无法使用箭头移动选择,直到我用鼠标单击列表:(。我尝试了 and 的不同组合focus_set()focus_force()但它不起作用。

在 Windows 7、OSX 10.7 和 Ubuntu 12.04 上使用 Python 2.7 检查

更新

如果“Treeview”更改为其他一些小部件,例如“Button”,则焦点正在工作。所以似乎我以某种方式错误地为 Treeview 设置了焦点。

4

1 回答 1

9

终于找到了一个解决方案 - 似乎Treeview小部件需要设置焦点两次:第一次是小部件本身,第二次是一个项目:

list.selection_set( items[ 0 ] )
list.focus_set()
list.focus( items[ 0 ] ) # this fixes a problem.
于 2012-06-30T21:13:02.897 回答