GTK+-3.0学习笔记(二)——Packing
作者:互联网
GTK+-3.0学习笔记(二)——Packing
参考自官方文档
本文基于前两篇文章,主要介绍GTK中的部件打包方式。
所有的打包方式见这篇文章
本文主要使用一个示例代码对Grid方式进行讲解
创建一个grid容器:gtk_grid_new()
为窗口增加子容器:gtk_container_add(GTK_CONTAINER(window), grid);
为Grid容器增加部件:gtk_grid_attach(GTK_GRID(grid), button, left, top, width, hight)
,位置由left
、top
决定,大小由width
、hight
决定
示例代码:
#include <gtk/gtk.h>
void printHello(GtkWidget *widget, gpointer data);
void activate(GtkApplication *app, gpointer data);
int main(int argc, char *argv[])
{
GtkApplication *app = gtk_application_new("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
int status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return status;
}
void activate(GtkApplication *app, gpointer data)
{
// 创建窗口
GtkWidget *window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(window), "Packing");
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
// 创建grid容器,并将其加入至窗口
GtkWidget *grid = gtk_grid_new();
gtk_container_add(GTK_CONTAINER(window), grid);
// 加入新按钮
GtkWidget *button = gtk_button_new_with_label("Button_1");
g_signal_connect(button, "clicked", G_CALLBACK(printHello), NULL);
gtk_grid_attach(GTK_GRID(grid), button, 0, 0, 1, 1);
button = gtk_button_new_with_label("Button_2");
g_signal_connect(button, "clicked", G_CALLBACK(printHello), NULL);
gtk_grid_attach(GTK_GRID(grid), button, 1, 0, 1, 1);
button = gtk_button_new_with_label("Quit");
g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_widget_destroy), window);
gtk_grid_attach(GTK_GRID(grid), button, 0, 1, 2, 1);
// 展示桌面
gtk_widget_show_all(window);
}
void printHello(GtkWidget *widget, gpointer data)
{
g_print("Hello world!\n");
}
标签:GTK,button,gtk,window,+-,Packing,grid,new 来源: https://blog.csdn.net/weixin_45206746/article/details/112059693