Why is the first toolbar button automatically selected when my gtk application loads?

StackOverflow https://stackoverflow.com/questions/894876

  •  23-08-2019
  •  | 
  •  

Question

When my gtk application loads, the first item on the toolbar is automatically selected (it is highlighted, it is pressed when it hit enter). This is just a minor problem, but I would prefer for nothing to be selected by default.

Here is some basic sample code:

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>
Was it helpful?

Solution

This is probably just GTK+ handing the focus to some widget, it likes to have a widget focused if possible.

You can use grab_focus() to move the focus elsewhere, to get the desired behavior. If the toolbar is all the widgetry you have, it might be hard to find a suitable focus "target", though.

OTHER TIPS

gtk.ToolButton inherits gtk.Bin.

gtk.Bin is an abstract base class defining a widget that is a container with just one child.

So button is a container and

btn.set_can_focus(False)

will not work. You need

btn.set_focus_chain([])

or for every button in toolbar

toolbar.foreach(lambda x: x.set_focus_chain([]))

You can always add a widget and make it invisible, and transfer focus to it. Though I don't really find this to be an issue or know of anyone else who did.

It has focus because it is the 1st widget in your app. If you use glade, you can set "Has focus" to 'Yes' to any other widget and the toolbar will automatically lose focus on startup.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top