Pygtk + Glade: No handler button fires click another button

Asked

Viewed 14 times

0

Hello, everyone! I am developing a Python application using Pygtk+ 3.0 with Glade 3.22.2 (and Python 3.8.1). Everything was working fine, but suddenly my program started to show strange behavior. I have two methods to manipulate button clicks:on_save_file() and on_open_file(). Those functions shall receive a Gtk.Entry as a parameter and open/save a file (through a Filedialog), and set the filename of that file to the Gtk.Entry supplied.

The strange part starts now: suddenly, a button that would open a file began to magically fire one click on the other two buttons in another window (and that are in another file .glade) and that save a file. The result is three Filedialogs appearing as if, in fact, the user had clicked on the three buttons at the same time.

To do some tests, I removed the signals from the button in question, but it continues firing the click on the other two buttons, even if he himself has no handler and does nothing else.

I also tried to get back some commits in the past, but the error persisted (and I’m pretty sure it didn’t happen before, as I’m also pretty sure I didn’t update anything on my machine).

Does anyone have any idea where this problem might be?

Thank you!


More details

Here are my three methods that manipulate the signals.

# class blahblahblah: ...
def open_file(self, use_last_path=True) -> str:
    """
    Show an Open File dialog and return the filename

    :param use_last_path: Whether or not to start at the last folder opened
    :return: the selected filename
    """
    file_chooser = Gtk.FileChooserDialog(
        title="Open...", action=Gtk.FileChooserAction.OPEN)
    file_chooser.add_buttons(
        "Cancel", Gtk.ResponseType.CANCEL, "Open", Gtk.ResponseType.OK)
    file_chooser.set_default_response(Gtk.ResponseType.OK)
    file_chooser.add_filter(self.csv_file_filter)
    if self.last_opened_path and use_last_path:
        file_chooser.set_current_folder(self.last_opened_path)
    response = file_chooser.run()
    filename = None
    if response == Gtk.ResponseType.OK:
        filename = file_chooser.get_filename()
        self.last_opened_path = os.path.dirname(os.path.abspath(filename))
    file_chooser.destroy()
    return filename

def on_save_file(self, entry: Gtk.Entry) -> None:
    """Show a Save File dialog and set the filename to the given entry"""

    file_chooser = Gtk.FileChooserDialog(
        title="Save...", action=Gtk.FileChooserAction.SAVE)
    file_chooser.add_buttons(
        "Cancel", Gtk.ResponseType.CANCEL, "Save", Gtk.ResponseType.OK)
    file_chooser.set_default_response(Gtk.ResponseType.OK)
    file_chooser.add_filter(self.csv_file_filter)
    response = file_chooser.run()
    if response == Gtk.ResponseType.OK:
        filename = file_chooser.get_filename()
        entry.set_text(filename)
    file_chooser.destroy()

def on_open_file(self, entry: Gtk.Entry) -> None:
    """Show an Open File dialog and set the filename to the given entry"""

    filename = self.open_file()
    if filename:
        entry.set_text(filename)

The button that makes everything weird and that should just open a file (but that, as I said, removed the sign on_open_file his).

<object class="GtkButton">
  <property name="label" translatable="yes">Open</property>
  <property name="visible">True</property>
  <property name="can_focus">True</property>
  <property name="receives_default">True</property>
</object>

The buttons that are magically clicked:

<object class="GtkButton">
  <property name="label" translatable="yes">Browse</property>
  <property name="visible">True</property>
  <property name="can_focus">True</property>
  <property name="receives_default">True</property>
  <signal name="clicked" handler="on_save_file" object="GA_output_matrix" swapped="no"/>
</object>

<object class="GtkButton">
  <property name="label" translatable="yes">Browse</property>
  <property name="visible">True</property>
  <property name="can_focus">True</property>
  <property name="receives_default">True</property>
  <signal name="clicked" handler="on_save_file" object="OPS_output_matrix" swapped="no"/>
</object>

Each button above is in a file .glade different and they are all loaded with the Gtk.Builder:

builder: Gtk.Builder = Gtk.Builder()
builder.add_from_file('./Views/main.glade')
builder.add_from_file('./Views/ga.glade')
builder.add_from_file('./Views/ops.glade')
if __name__ == '__main__':
    builder.connect_signals(Handler())
    window = builder.get_object('main_window')
    window.show_all()
    Gtk.main()

1 answer

0

I don’t know exactly why, but adding an ID to each button solved the problem. Then the buttons became:

<object class="GtkButton" id="um_id_semantico_1">
  <property name="label" translatable="yes">Open</property>
  <property name="visible">True</property>
  <property name="can_focus">True</property>
  <property name="receives_default">True</property>
</object>
<object class="GtkButton" id="um_id_semantico_2">
  <property name="label" translatable="yes">Browse</property>
  <property name="visible">True</property>
  <property name="can_focus">True</property>
  <property name="receives_default">True</property>
  <signal name="clicked" handler="on_save_file" object="GA_output_matrix" swapped="no"/>
</object>

<object class="GtkButton" id="um_id_semantico_3">
  <property name="label" translatable="yes">Browse</property>
  <property name="visible">True</property>
  <property name="can_focus">True</property>
  <property name="receives_default">True</property>
  <signal name="clicked" handler="on_save_file" object="OPS_output_matrix" swapped="no"/>
</object>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.