Note that there are some explanatory texts on larger screens.

plurals
  1. POgtk - close window form with button in C
    text
    copied!<p>I have a main window with a menu wich opens another window. This secondary window has a button Close. That button has the signal clicked connected. My problem is that I don't know how to close/destroy that parent window. I have tried with gtk_widget_destroy, but an error appears because window is not a widget .... I haven't found any function to destroy the parent window ....</p> <p>Can anyone show me the way, please? Thanks in advance.</p> <p><em>-----------------------------------------------</em></p> <p>Ok. I post a piece of code. When I execute the program I click in "Open window" button. A new window is openned with one button "Close". If I click in "Close" button I get next error in terminal: (Windows:13801): Gtk-CRITICAL **: gtk_widget_destroy: assertion `GTK_IS_WIDGET (widget)' failed</p> <p>The code is:</p> <pre><code>#include &lt;stdlib.h&gt; #include &lt;gtk/gtk.h&gt; #include &lt;gdk/gdkkeysyms.h&gt; void open_window(GtkWidget *widget, gpointer window); void close_window(GtkWidget *widget, gpointer window); int main( int argc, char *argv[]) { GtkWidget *window; GtkWidget *fixed; GtkWidget *button; gtk_init(&amp;argc, &amp;argv); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(window), "Windows"); gtk_window_set_default_size(GTK_WINDOW(window), 230, 150); gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); fixed = gtk_fixed_new(); gtk_container_add(GTK_CONTAINER(window), fixed); button = gtk_button_new_with_label("Open window"); gtk_fixed_put(GTK_FIXED(fixed), button, 50, 50); gtk_widget_set_size_request(button, 80, 35); g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(open_window), G_OBJECT(window)); g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL); gtk_widget_show_all(window); gtk_main(); return 0; } void open_window(GtkWidget *widget, gpointer window) { GtkBuilder *builder; GtkWidget *secondWindow = NULL; builder = gtk_builder_new (); gtk_builder_add_from_file (builder, "secondWindow.glade", NULL); secondWindow = GTK_WIDGET (gtk_builder_get_object (builder, "secondWindow")); gtk_builder_connect_signals (builder, NULL); g_object_unref (G_OBJECT (builder)); gtk_window_set_modal(GTK_WINDOW(secondWindow), TRUE); gtk_widget_show_all(secondWindow); } void close_window(GtkWidget *widget, gpointer window) { gtk_widget_destroy(GTK_WIDGET(window)); } </code></pre> <p>In file "secondWindow.glade" is defined a window, a table and a button placed in the middle cell of the table. Also, it is defined a handle for the "clicked" event button named "close_window".</p> <p>Link to glade file if anyone wants to execute it: <a href="https://sites.google.com/site/marvalsiteimages/secondWindow.glade" rel="nofollow">https://sites.google.com/site/marvalsiteimages/secondWindow.glade</a></p> <p>I hope this could help you to understand my problem. Thansk.</p> <p><em>-------------------------------------------------</em></p> <p>Final code based on the response:</p> <pre><code>void open_window(GtkWidget *widget, gpointer window) { GtkBuilder *builder; GtkWidget *secondWindow = NULL; GtkWidget *closeButton = NULL; builder = gtk_builder_new (); gtk_builder_add_from_file (builder, "secondWindow.glade", NULL); secondWindow = GTK_WIDGET (gtk_builder_get_object (builder, "secondWindow")); closeButton = GTK_WIDGET (gtk_builder_get_object (builder, "closeWindowButton")); g_signal_connect (G_OBJECT (closeButton), "clicked", G_CALLBACK (close_window), G_OBJECT (secondWindow)); // here is the magic: the callback will get the window to close g_object_unref (G_OBJECT (builder)); gtk_window_set_modal(GTK_WINDOW(secondWindow), TRUE); gtk_widget_show_all(secondWindow); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload