我已经用C ++编写了GUI。
#include <gtk/gtk.h>
#include <unistd.h>
#include <cstddef>
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
int main(){
GtkWidget *_LinuxWindow, *_Box, *_Button;
int argC = 0;
char** argV;
// Setup the window and fixed grid
gtk_init(&argC,&argV);
_LinuxWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
_Box = gtk_fixed_new();
// Set the title
gtk_window_set_title(GTK_WINDOW(_LinuxWindow),"Title");
// Finish up
gtk_widget_show(_LinuxWindow);
g_signal_connect(G_OBJECT(_LinuxWindow),"destroy",G_CALLBACK(gtk_main_quit), NULL);
// Add controls
_Button = gtk_button_new_with_label("Click Me!");
gtk_fixed_put(GTK_FIXED(_Box),_Button,20,20);
gtk_fixed_move(GTK_FIXED(_Box),_Button,20,20);
gtk_widget_show(_Box);
gtk_widget_set_size_request(_Button,30,100);
// Create a dialog
GtkWidget *dialog;
dialog = gtk_message_dialog_new(GTK_WINDOW(_LinuxWindow), GTK_DIALOG_DESTROY_WITH_PARENT,GTK_MESSAGE_INFO,GTK_BUTTONS_OK_CANCEL,"OK or Cancel?",NULL,g_strerror(errno));
gint ret = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(GTK_WIDGET(dialog));
printf("%i", ret);
// Add the fixed grid and go the to the main window loop
gtk_container_add(GTK_CONTAINER(_LinuxWindow),_Box);
gtk_widget_show(_Box);
gtk_widget_show_all(_LinuxWindow);
gtk_main();
return 0;
}
当我使用运行它时
g++ -o out-withoutCross without-cross.cpp -lX11 `pkg-config --cflags gtk+-3.0`pkg-config --libs gtk+-3.0`
将显示一个消息框和一个窗口。问题是打开消息框时,窗口中的控件没有出现。我认为这可能是因为消息框在与主窗口相同的线程上运行。有什么方法可以向GTK添加多线程吗?
退出主窗口后,打印到控制台的消息框的返回值也在运行。
问题是您的代码结构。.]]
#include <gtk/gtk.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main() { GtkWidget *_LinuxWindow, *_Box, *_Button; int argC = 0; char **argV; // Setup the window and fixed grid gtk_init(&argC, &argV); _LinuxWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL); _Box = gtk_fixed_new(); // Set the title gtk_window_set_title(GTK_WINDOW(_LinuxWindow), "Title"); // Finish up gtk_widget_show_all(_LinuxWindow); g_signal_connect(G_OBJECT(_LinuxWindow), "destroy", G_CALLBACK(gtk_main_quit), NULL); // Add controls _Button = gtk_button_new_with_label("Click Me!"); gtk_fixed_put(GTK_FIXED(_Box), _Button, 20, 20); gtk_fixed_move(GTK_FIXED(_Box), _Button, 20, 20); gtk_widget_set_size_request(_Button, 30, 100); gtk_container_add(GTK_CONTAINER(_LinuxWindow), _Box); gtk_widget_show_all(_LinuxWindow); // Create a dialog GtkWidget *dialog; dialog = gtk_message_dialog_new( GTK_WINDOW(_LinuxWindow), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, GTK_BUTTONS_OK_CANCEL, "OK or Cancel?", NULL); gint ret = gtk_dialog_run(GTK_DIALOG(dialog)); gtk_widget_destroy(GTK_WIDGET(dialog)); printf("%i", ret); // Add the fixed grid and go the to the main window loop gtk_main(); return 0; }
gtk_dialog_run是一项阻止功能,因此它将等待,直到它从对话框中获得响应。然后继续进行]]
在您的代码中,仅在运行对话框窗口(这是一个阻塞调用)之后才将按钮添加到窗口,因此在执行之后,按钮将被添加到窗口中