【发布时间】:2021-04-10 13:29:39
【问题描述】:
由于我是 glade 和 C 的新手,我正在尝试构建一个非常简单的 GUI,它有一个输入框、一个标签和一个按钮。当按下按钮时,文本框中存在的任何值都会显示在标签中。这是我的空地,
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkWindow" id="windows1">
<property name="can_focus">False</property>
<child>
<placeholder/>
</child>
<child>
<object class="GtkFixed">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkButton" id="button1">
<property name="label" translatable="yes">button</property>
<property name="width_request">100</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_clicked" swapped="no"/>
</object>
<packing>
<property name="x">182</property>
<property name="y">146</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entry1">
<property name="width_request">100</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
</object>
<packing>
<property name="x">68</property>
<property name="y">45</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label1">
<property name="width_request">100</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">label</property>
</object>
<packing>
<property name="x">321</property>
<property name="y">44</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
这是按下按钮时在终端中打印 clicked 的 C 逻辑。
#include <gtk/gtk.h>
void on_clicked(GtkButton * b, gpointer data)
{
(void)b;
(void)data;
printf("clicked");
}
int main (int argc, char *argv[])
{
GtkBuilder *builder;
GtkWidget *window;
gtk_init(&argc, &argv);
builder = gtk_builder_new();
gtk_builder_add_from_file (builder, "example.glade", NULL);
window = GTK_WIDGET(gtk_builder_get_object(builder, "window1"));
gtk_builder_connect_signals(builder, NULL);
g_object_unref(builder);
gtk_widget_show(window);
gtk_main();
return 0;
}
// called when window is closed
void on_window_main_destroy()
{
gtk_main_quit();
}
我使用编译c代码没有任何错误
gcc main.c $(pkg-config --cflags --libs gtk+-3.0) -o main -export-dynamic
但是当我运行时我得到这个警告但没有 gui
Gtk-CRITICAL **: : gtk_widget_show: 断言 'GTK_IS_WIDGET (widget)' 失败
我该如何解决这个问题?第二个问题是如何从文本框中获取值并使用 c 将其显示在标签中。
【问题讨论】: