0

I have created a two-window app with Glade and pygobject. I have a window with an open-window-button that opens the second window. The second window has a close button. When I click the close-button it prints something to the terminal and triggers a ".hide_on_delete()" function, which closes the second window.

When I click the open-window-button again it opens the window again with a simple ".show()". And I can do this as often as I want to.

However there's a problem when I close the second window with the "x" in the upper-right corner instead of with the close-button I created. In glade I set up the "destroy" event and gave it the correct function name. When I close the second window via the "x" It even prints out "Clicked on the X button" and closes the second window. But when I open it up again I just get a black square with no window decoration and when I click on it I get this error message:

(main.py:11083): Gtk-CRITICAL **: 21:37:50.904: gtk_widget_get_window: assertion 'GTK_IS_WIDGET (widget)' failed

(main.py:11083): Gtk-CRITICAL **: 21:37:50.905: gtk_widget_translate_coordinates: assertion 'GTK_IS_WIDGET (dest_widget)' failed

(main.py:11083): Gtk-CRITICAL **: 21:37:50.905: gtk_widget_get_window: assertion 'GTK_IS_WIDGET (widget)' failed

(main.py:11083): Gtk-CRITICAL **: 21:37:50.905: gtk_widget_translate_coordinates: assertion 'GTK_IS_WIDGET (dest_widget)' failed

(main.py:11083): Gtk-CRITICAL **: 21:37:50.905: gtk_widget_get_settings: assertion 'GTK_IS_WIDGET (widget)' failed
/usr/lib/python3.6/site-packages/gi/overrides/Gtk.py:1612: Warning: g_object_get: assertion 'G_IS_OBJECT (object)' failed
  return _Gtk_main(*args, **kwargs)

(main.py:11083): Gtk-CRITICAL **: 21:37:50.905: gtk_widget_get_window: assertion 'GTK_IS_WIDGET (widget)' failed

(main.py:11083): Gtk-CRITICAL **: 21:37:50.905: gtk_widget_translate_coordinates: assertion 'GTK_IS_WIDGET (dest_widget)' failed

(main.py:11083): Gtk-CRITICAL **: 21:37:50.977: gtk_widget_get_window: assertion 'GTK_IS_WIDGET (widget)' failed

(main.py:11083): Gtk-CRITICAL **: 21:37:50.977: gtk_widget_translate_coordinates: assertion 'GTK_IS_WIDGET (dest_widget)' failed

(main.py:11083): Gtk-CRITICAL **: 21:37:50.977: gtk_widget_get_window: assertion 'GTK_IS_WIDGET (widget)' failed

(main.py:11083): Gtk-CRITICAL **: 21:37:50.977: gtk_widget_get_window: assertion 'GTK_IS_WIDGET (widget)' failed

Here are my functions:

def on_second_window_destroy(self, *args):
    print("x Button clicked")
    app.builder.get_object("second_window").hide_on_delete() 

def on_new_window_button_clicked(self, *args):
    app.builder.get_object("second_window").show()

def on_close_button_clicked(self, *args):
    print("close Button clicked")
    app.builder.get_object("second_window").hide_on_delete() 

The close button and the "x" button trigger the exact same function. Why does one let me reopen the window again and the other one doesn't? I really do not understand what's wrong.

Thanks so much in advance for any help!

casualcoder
  • 231
  • 2
  • 11
  • 1
    I had to delete my answer, as it is probably absolutely wrong. Concerning system `x`, did you connect to "delete-event", "destroy" or "destroy-event? Because docs say `Connecting gtk_widget_hide_on_delete() to this signal ("delete-event") will cause the window to be hidden instead, so that it can later be shown again without reconstructing it` – Alexander Dmitriev Jun 03 '18 at 08:54
  • Thanks again for your help. I changed the "destroy" to a "delete-event" signal, but I experience the same error. However I created a MCVE and you can find it here: https://pastebin.com/efXHZqNP I double checked it, but it still isn't working. "Hiding" the window via the close button and opening it again works, hiding it via the "x" button and reopening still gives an error. Do you have any idea as to why that is? – casualcoder Jun 03 '18 at 11:17

1 Answers1

1

As I mentioned in comments, you should be connected to "delete-event" signal. It's callback looks like this:

gboolean
user_function (GtkWidget *widget,
               GdkEvent  *event,
               gpointer   user_data)

You must return True to stop other handlers from being invoked for the event. False to propagate the event further.

Gtk.Widget.hide_on_delete() always returns true, that's why direct connection of this function to signal stops emission. Thus, write return app.builder.get_object("second_window").hide_on_delete() and it's done.

Alexander Dmitriev
  • 2,483
  • 1
  • 15
  • 20
  • Ahhh you are correct. Now it works. I didn't know you had to return it with the function instead of just calling it. Thank you so much! – casualcoder Jun 03 '18 at 12:54