gpt4 book ai didi

c - 如何在单个窗口中显示gtk中的文本和按钮?

转载 作者:行者123 更新时间:2023-11-30 15:56:06 26 4
gpt4 key购买 nike

我想在我的窗口和“下一步”按钮上显示文本,但是当我运行代码时,它给了我错误。

使用下一个按钮,我想移动到下一个窗口,在该窗口中显示其他内容。有人可以告诉我如何做到这一点吗?

(try:1914): Gtk-WARNING **: Attempting to add a widget with type GtkVBox to a GtkWindow, but as a GtkBin subclass a GtkWindow can only contain one widget at a time; it already contains a widget of type GtkLabel

我的代码是:

#include <gtk/gtk.h>

int main (int argc, char *argv[])
{

GtkWidget *window;
GtkWidget *next;

GtkWidget *label;
GtkWidget *vbox;
GtkWidget *hbox;
GtkWidget *halign;
GtkWidget *valign;


gtk_init (&argc, &argv);

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Center");//title position
gtk_window_set_default_size(GTK_WINDOW(window),600,500);//size
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);//position
label = gtk_label_new("Cold was my soul\n\
Untold was the pain\n\
I faced when you left me\n\
A rose in the rain....\n\
So I swore to the razor\n\
That never, enchained\n\
Would your dark nails of faith\n\
Be pushed through my veins again\n\
\n\
Bared on your tomb\n\
I'm a prayer for your loneliness\n\
And would you ever soon\n\
Come above onto me?\n\
For once upon a time\n\
On the binds of your lowliness\n\
I could always find the slot for your sacred key ");

gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_CENTER);
gtk_container_add(GTK_CONTAINER(window), label);
vbox = gtk_vbox_new(FALSE, 5);

valign = gtk_alignment_new(0, 1, 0, 0);
gtk_container_add(GTK_CONTAINER(vbox), valign);
gtk_container_add(GTK_CONTAINER(window), vbox);

hbox = gtk_hbox_new(TRUE, 3);

next = gtk_button_new_with_label("Next");
gtk_widget_set_size_request(next, 70, 30);
gtk_container_add(GTK_CONTAINER(hbox), next);
// close = gtk_button_new_with_label("Close");
// gtk_container_add(GTK_CONTAINER(hbox), close);

halign = gtk_alignment_new(1, 0, 0, 0);
gtk_container_add(GTK_CONTAINER(halign), hbox);

gtk_box_pack_start(GTK_BOX(vbox), halign, FALSE, FALSE, 0);
g_signal_connect(G_OBJECT(next), "clicked",
G_CALLBACK(gtk_main_quit), G_OBJECT(window));

gtk_widget_show_all(window);

g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);


gtk_main ();

return 0;
}

最佳答案

消息非常清楚:GtkWindow 只能包含一个子窗口小部件,但您已经用标签填充了它。要实现您想要的效果,您必须将 vbox (能够包含多个小部件)添加到窗口,然后将 label 添加到 vbox

关于c - 如何在单个窗口中显示gtk中的文本和按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11489607/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com