I have a simple Python + Tkinter application that displays a list of 10 items:
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()
Is it possible to modify it so after application starts, list will have focus and i can move selection via up and down arrows? After app starts, app's window has focus, but i can't move selection with arrows until i click list with mouse :(. I tried different combinations of focus_set()
and focus_force()
, but it's not working.
Checked with Python 2.7 on Windows 7, OSX 10.7 and Ubuntu 12.04
UPDATE
If "Treeview" is changed to some other widget, for example to "Button", the focus is working. So it's seems that i set focus for Treeview somehow incorrectly.
Focus is used to refer to the widget or window which is currently accepting input. Widgets can be used to restrict the use of mouse movement, grab focus, and keystrokes out of the bounds. However, if we want to focus a widget so that it gets activated for input, then we can use focus. set() method.
Build A Paint Program With TKinter and Python Let us suppose that there are some widgets present in an application such that we have to focus on a particular widget. By using the focus_set() method, we can activate the focus on any widget and give them priority while executing the application.
foreground − Foreground color for the widget. This can also be represented as fg.
config is used to access an object's attributes after its initialisation. For example, here, you define. l = Label(root, bg="ivory", fg="darkgreen") but then you want to set its text attribute, so you use config : l.
Found a solution at last - seems that Treeview
widget need to be set focus two times: first for the widget itself, and second for an item:
list.selection_set( items[ 0 ] )
list.focus_set()
list.focus( items[ 0 ] ) # this fixes a problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With