0

When I create a GtkFileChooserDialog in Glade and then connect that to a Python class using Gtk.Builder, how do I tell it which is the default button?

Currently, my 'Open' button is the stock gtk-open button and when it's clicked, run() returns 0. But double-clicking on a file or selecting a file and pressing Enter doesn't do anything. As far as I can see, this is because the dialog doesn't know what the default button is in Glade, possibly because the 'Open' button isn't using one of GTK_RESPONSE_ACCEPT, GTK_RESPONSE_OK, GTK_RESPONSE_YES or GTK_RESPONSE_APPLY as its response code; but how do I set the response code to one of these when the button is created through Glade?

I'm using Gtk 3, Python 3 and PyGObject (ie gi.repository).

Edit: Here is the relevant bit of the Glade file:

  <object class="GtkFileChooserDialog" id="file_chooser">
    <property name="can_focus">False</property>
    <property name="modal">True</property>
    <property name="type_hint">dialog</property>
    <property name="select_multiple">True</property>
    <child internal-child="vbox">
      <object class="GtkBox">
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <property name="spacing">2</property>
        <child internal-child="action_area">
          <object class="GtkButtonBox">
            <property name="can_focus">False</property>
            <property name="layout_style">end</property>
            <child>
              <object class="GtkButton" id="button1">
                <property name="label">gtk-cancel</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <property name="use_stock">True</property>
              </object>
              <packing>
                <property name="expand">True</property>
                <property name="fill">True</property>
                <property name="position">0</property>
              </packing>
            </child>
            <child>
              <object class="GtkButton" id="button2">
                <property name="label">gtk-open</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <property name="use_stock">True</property>
              </object>
              <packing>
                <property name="expand">True</property>
                <property name="fill">True</property>
                <property name="position">1</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">False</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <placeholder/>
        </child>
      </object>
    </child>
    <action-widgets>
      <action-widget response="-1">button1</action-widget>
      <action-widget response="-5">button2</action-widget>
    </action-widgets>
  </object>
Tom
  • 7,269
  • 1
  • 42
  • 69

2 Answers2

2

You can set the response id to a button in Glade. (General>Response ID) The response types are here.

GTK_RESPONSE_NONE is -1

GTK_RESPONSE_REJECT is -2

GTK_RESPONSE_ACCEPT is -3

... so on until ...

GTK_RESPONSE_HELP is -11

theGtknerd
  • 3,647
  • 1
  • 13
  • 34
  • Although, this doesn't help with the original problem, which is that double-clicking on a file or selecting a file and pressing Enter, doesn't do anything. – Tom Nov 16 '17 at 10:05
  • 1
    The rest of your program is probably not setup right. Edit your question with a [MCVE](https://stackoverflow.com/help/mcve) – theGtknerd Nov 16 '17 at 11:58
0

Try

dialog.set_default_response(Gtk.ResponseType.YES)

Can’t tell for sure how to change response code on Glade, but it shouldn't matter to you if you just write the handler for your`s Gtk.ResponseType.YES code

EDIT:

If you were not using glade things like that are wired already

dialog = builder.get_object("file_chooser")
dialog.connect('file-activated', self.file_selected)

def file_selected(self, dialog):
    dialog.emit('response', Gtk.ResponseType.OK)

It is important to resume operations after You run() your dialog so emit this signal and do nothing more

Giedrius.S
  • 70
  • 7