我正在做一个小项目,我有一个全屏GUI(Glade,GTK和C),需要给用户一个关闭屏幕的方法(显然窗口管理器因窗口全屏而无法使用)。
当我尝试编译这段代码时,问题就会出现问题
//close sidebar1
void on_window_sidebar1_back_clicked()
{
gtk_window_close (GtkWindow *window);
}
我收到以下错误
/usr/bin/arm-linux-gnueabihf-gcc -c -g -O0 -Wall -pthread -pipe src/main.c `pkg-config --cflags --libs gtk+-3.0` -o main.o
src/main.c: In function 'on_window_sidebar1_back_clicked':
src/main.c:61:2: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'gtk_window_close'
gtk_window_close (GtkWindow *window);
^
src/main.c:61:20: error: expected expression before 'GtkWindow'
gtk_window_close (GtkWindow *window);
^
makefile:30: recipe for target 'main.o' failed
make: *** [main.o] Error 1
我尝试按照文档here进行操作,但似乎失败了
有没有人有任何想法?
PS。
这是完整的main.c如果有人有兴趣。这是我第一次使用gtk尝试C,所以不要太苛刻,谢谢(=
#include <gtk/gtk.h>
GtkWidget *g_lbl_test;
GtkWidget *g_lbl_count;
int main(int argc, char *argv[])
{
GtkBuilder *builder;
GtkWidget *window;
gtk_init(&argc, &argv);
builder = gtk_builder_new();
gtk_builder_add_from_file (builder, "glade/window_main.glade", NULL);
window = GTK_WIDGET(gtk_builder_get_object(builder, "window_main"));
gtk_builder_connect_signals(builder, NULL);
//pointers for the labels (...used in button press)
g_lbl_test = GTK_WIDGET(gtk_builder_get_object(builder, "lbl_test"));
g_lbl_count = GTK_WIDGET(gtk_builder_get_object(builder, "lbl_count"));
g_object_unref(builder);
gtk_widget_show(window);
//gtk_window_fullscreen (GtkWindow *window); //dont know how to use this, same with gtk_window_close
gtk_main();
return 0;
}
//button press (just a test to make sure the window didnt freeze)
void on_btn_count_clicked()
{
static unsigned int count= 0;
char str_count[30] = {0};
gtk_label_set_text(GTK_LABEL(g_lbl_test), "Test success!");
count++;
sprintf(str_count, "%d", count);
gtk_label_set_text(GTK_LABEL(g_lbl_count), str_count);
}
//sidebar1
void on_sidebar_1_clicked()
{
GtkBuilder *builder;
GtkWidget *window;
builder = gtk_builder_new();
gtk_builder_add_from_file (builder, "glade/window_main.glade", NULL);
window = GTK_WIDGET(gtk_builder_get_object(builder, "window_sidebar1"));
gtk_widget_show(window);
}
//close sidebar1
void on_window_sidebar1_back_clicked()
{
gtk_window_close (GtkWindow *window);
}
// called on closing window
void on_window_main_destroy()
{
gtk_main_quit();
}
答案 0 :(得分:2)
您可能应该致电
gtk_close_window(window);
但是window
应该来自某个地方。
(您可以使用带有连接的Gtk闭包,或带有客户端数据的回调等,或以某种方式存储window
在全局变量等中......)
阅读有关C编程的更多信息,然后阅读有关callbacks和closures的内容,然后详细了解GTK和Gobject的signal机制。
(你的代码显示了很多混乱;我建议在阅读好的C编程书之后阅读Getting started with GTK章节;顺便说一句,我建议先写一些 not < / em>全屏,只是稍后改进它)
不要忘记在编译时启用所有警告和调试信息,即使用gcc -Wall -Wextra -g
(以及其他参数,可能使用$(pkg-config --cflags gtk+-3.0)
和$(pkg-config --libs gtk+-3.0)
进行编译联)。
答案 1 :(得分:-1)
注意!这不遵循任何工业标准,并且存在多个缺陷。我没有从中赚钱或成名,所以我唯一的要求就是它有效。阅读本文时请考虑到这一点!
我用我学到的东西编辑了我的代码,这似乎有效:
void on_sidebar_1_clicked(int argc, char *argv[])
{
GtkBuilder *builder;
gtk_init(&argc, &argv);
builder = gtk_builder_new();
gtk_builder_add_from_file (builder, "glade/window_sidebar1.glade", NULL); //a whole own file for the second window
secondWindow = GTK_WIDGET(gtk_builder_get_object(builder, "window_sidebar1")); //secondWindow is defined at the start of main.c like this: "GtkWidget *secondWindow = NULL;" so that both callbacks can use it.
gtk_builder_connect_signals(builder, NULL);
gtk_widget_show(secondWindow);
gtk_main();
}
void on_sidebar1_back_clicked()
{
gtk_widget_destroy(GTK_WIDGET(secondWindow)); //here we call the widget to be closed instead of the window. Seems to work a lot better than the gtk_window_close, dunno why.
}
这段代码写得很快(而且很生气),所以它有点粗糙。我对我的glade文件也进行了一些更改,如果有人需要参考那些,请询问。据我所知,这是我的问题的答案。另外,在你开始做这种事情之前,我建议学习C和GTK(比如Basile Starynkevitch说的),我没有时间也没有动力这样做。