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>

      

+1


a source to share


4 answers


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.

+3


a source


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([]))

      

+2


a source


You can always add a widget and make it invisible and give focus to it. Although I don't really think this is a problem or know someone else who has done it.

+1


a source


It has focus because it is the first widget in your application. If you are using a glade, you can set "Focused" to "Yes" on any other widget, and the toolbar will automatically lose focus on startup.

0


a source







All Articles