1

我希望我的标题在顶部,下一个按钮在右下角。但我无法做到这一点。它显示在右中下,并且标题略高于底部。使用下一个按钮,我想移动到其他窗口,我在这里打电话gtk_main_quit()。我的代码是:

#include <gtk/gtk.h>

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

        GtkWidget *window;
        GtkWidget *next;

        GtkWidget *align;
        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
        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(FALSE, 3);

        next = gtk_button_new_with_label("Next");
        gtk_widget_set_size_request(next, 70, 30);
        gtk_container_add(GTK_CONTAINER(hbox), next);
        halign = gtk_alignment_new(1, 0, 0, 0);
        gtk_container_add(GTK_CONTAINER(halign), hbox);
        align = gtk_alignment_new(0, 0, 1, 0);

        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));
        label = gtk_label_new("No. of projects are:\n");

        gtk_container_add(GTK_CONTAINER(vbox), label);

        gtk_widget_show_all(window);

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


        gtk_main ();

        return 0;
}
4

1 回答 1

1

vbox从上到下添加元素,因此如果您希望标签出现在顶部,您应该移动这些行:

    label = gtk_label_new("No. of projects are:\n");  
    gtk_container_add(GTK_CONTAINER(vbox), label);

所以标签是添加到的第一个对象vbox,就在你初始化它之后。然后“下一步”按钮也会移动到右下角。

GTK+ 2.0 教程中有一个章节是关于这个的:Packing Widgets

于 2012-07-15T13:08:42.493 回答