gtk -如何打开一个窗口,并关闭旧窗口时,我按下一个按钮在C?

r7knjye2  于 2023-11-16  发布在  其他
关注(0)|答案(1)|浏览(99)

我是gtk的新手,我想用这个GUI做一个完整的程序。这里的问题是我如何打开一个窗口并关闭旧窗口,比如如果我按下一个名为“新窗口”的按钮,我打开一个新窗口,我关闭或隐藏旧窗口,我可以使用新窗口。
下面是一些代码来理解我所说的:

#include <gtk/gtk.h>
#include <glib/gstdio.h>
#include <stdlib.h>

static void fc_open_window(GtkWidget *widget,gpointer window)
{
  gtk_widget_hide(window);
  system("./NewWindow");
  gtk_window_destroy(window);
}

static void activate(GtkApplication *app, gpointer user_data)
{
  GtkWidget *window;
  GtkWidget *grid;
  GtkWidget *button;

  window = gtk_application_window_new(app);
  gtk_window_set_title(GTK_WINDOW(window), "Window");
  
  grid = gtk_grid_new();
  gtk_grid_set_row_homogeneous(grid,true);
  gtk_grid_set_column_homogeneous(grid,true);

  gtk_window_set_child(GTK_WINDOW(window), grid);

  button = gtk_button_new_with_label("New Window");
  g_signal_connect(button, "clicked", G_CALLBACK(fc_open_window), window);
  gtk_grid_attach(GTK_GRID(grid), button, 0, 0, 1, 1);

  button = gtk_button_new_with_label("Quit");
  g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_window_destroy), window);
  gtk_grid_attach(GTK_GRID(grid), button, 0, 1, 2, 1);

  gtk_widget_show(window);

}

int main (int argc,char **argv)
{
  GtkApplication *app;
  int status;

  app = gtk_application_new("gtk.main.window1", G_APPLICATION_FLAGS_NONE);
  g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
  status = g_application_run(G_APPLICATION(app), argc, argv);
  g_object_unref(app);

  return status;
}https://stackoverflow.com/questions/29068510/how-to-get-window-to-change-on-button-click-in-c

字符串
我使用gtk_window_destroy(window)来使用新的函数,但没有什么特别的。我试图把“NewWindow”的代码放在一个函数中,但更多的是相同的。NewWindow的代码就像官方页面中的gtk example-1.c(你有这里的例子https://docs.gtk.org/gtk4/getting_started.html)。如果你有任何想法,我的问题,请把它放在评论中。
抱歉,我的英语不好。

laik7k3q

laik7k3q1#

#include <gtk/gtk.h>

int fc_open_window(GtkWidget *widget,gpointer window)
{
    GError *error = NULL;
    gchar *argv[] = {"NewWindow", NULL};

    if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_DEFAULT, NULL, NULL, NULL, &error)) {
        g_print("Error starting the new program: %s\n", error->message);
        g_error_free(error);
        return 1;
    }

  gtk_window_destroy(GTK_WINDOW(window));

}

static void activate(GtkApplication *app, gpointer user_data)
{
  GtkWidget *window;
  GtkWidget *grid;
  GtkWidget *button;

  window = gtk_application_window_new(app);
 gtk_window_set_title(GTK_WINDOW(window), "Window");

 grid = gtk_grid_new();
 gtk_grid_set_row_homogeneous(GTK_GRID(grid),true);
 gtk_grid_set_column_homogeneous(GTK_GRID(grid),true);

 gtk_window_set_child(GTK_WINDOW(window), grid);

 button = gtk_button_new_with_label("New Window");
 g_signal_connect(button, "clicked", G_CALLBACK(fc_open_window), window);
 gtk_grid_attach(GTK_GRID(grid), button, 0, 0, 1, 1);

 button = gtk_button_new_with_label("Quit");
 g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_window_destroy), window);
 gtk_grid_attach(GTK_GRID(grid), button, 0, 1, 2, 1);

 gtk_widget_set_visible(window,TRUE);

}

字符串
要从GTK应用程序中启动一个程序,可以选择g_spawn_blog()方法。
但是,您必须确保您要启动的程序使用了不同的应用程序ID。否则,在D-Bus上的注册将出现问题。

相关问题