Why is the first toolbar button automatically selected when loading gtk app?
When the gtk app is loaded, the first item in the toolbar is automatically selected (it is highlighted, it is clicked when hit). This is only a minor issue, but I would prefer that nothing is selected by default.
Here's a basic example:
import gtk
import gtk.glade
window_tree = gtk.glade.XML('testtoolbar.glade')
gtk.main()
testtoolbar.glade
<?xml version="1.0"?>
<glade-interface>
<!-- interface-requires gtk+ 2.16 -->
<!-- interface-naming-policy project-wide -->
<widget class="GtkWindow" id="window1">
<property name="width_request">200</property>
<property name="visible">True</property>
<child>
<widget class="GtkToolbar" id="toolbar1">
<property name="visible">True</property>
<child>
<widget class="GtkToolButton" id="toolbutton1">
<property name="visible">True</property>
<property name="label" translatable="yes">toolbutton1</property>
<property name="use_underline">True</property>
<property name="stock_id">gtk-new</property>
</widget>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<widget class="GtkToolButton" id="toolbutton2">
<property name="visible">True</property>
<property name="label" translatable="yes">toolbutton2</property>
<property name="use_underline">True</property>
<property name="stock_id">gtk-open</property>
</widget>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
</widget>
</child>
</widget>
</glade-interface>
a source to share
It's probably just GTK + giving focus to some widget, it likes to see the widget if possible.
You can use grab_focus()
to move focus elsewhere to get the desired behavior. However, if the toolbar has all the widgets, it can be difficult to find a suitable "target" for focusing.
a source to share
gtk.ToolButton inherits gtk.Bin.
gtk.Bin is an abstract base class that defines a widget that is a container with only one child .
So the button is a container and
btn.set_can_focus(False)
will not work. You need
btn.set_focus_chain([])
or for each button on the toolbar
toolbar.foreach(lambda x: x.set_focus_chain([]))
a source to share