我用GTK写了一个简单的计算器,我用一个网格来放置所有的数字按钮。我希望我的等号按钮和两个普通按钮一样高,但是我想把它的底部和它的兄弟按钮的底部对齐。我怎么才能做到这一点呢?我应该使用另一种类型的小部件而不是使用网格吗?
static void
activate (GtkApplication *app,
gpointer user_data)
{
//declaration of each widget (in this case, buttons)
GtkWidget *window;
GtkWidget *grid_numbers;
GtkWidget *zero;
GtkWidget *one;
GtkWidget *comma;
GtkWidget *mod;
GtkWidget *plus;
GtkWidget *equal;
GtkWidget *squared;
GtkWidget *minus;
GtkWidget *mult;
GtkWidget *sqrt;
GtkWidget *divide;
GtkWidget *two;
GtkWidget *three;
GtkWidget *four;
GtkWidget *five;
GtkWidget *six;
GtkWidget *seven;
GtkWidget *eight;
GtkWidget *nine;
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "The C Calculator");
gtk_window_set_default_size (GTK_WINDOW (window), 300, 490);
grid_numbers = gtk_grid_new();
gtk_widget_set_halign(grid_numbers, GTK_ALIGN_CENTER);
gtk_widget_set_valign(grid_numbers, GTK_ALIGN_END);
gtk_grid_set_row_homogeneous(GTK_GRID(grid_numbers), true);
gtk_grid_set_column_homogeneous(GTK_GRID(grid_numbers), true);
//gtk_grid_set_column_spacing(GTK_GRID(grid_numbers), 100);
//gtk_grid_set_row_spacing(GTK_GRID(grid_numbers), 100);
gtk_window_set_child(GTK_WINDOW(window), grid_numbers);
zero = gtk_button_new_with_label("0");
//implement logic
gtk_grid_attach(GTK_GRID(grid_numbers), zero, 0, 0, 10, 10);
g_signal_connect(zero, "clicked", G_CALLBACK(zeroFun), NULL);
comma = gtk_button_new_with_label(",");
gtk_grid_attach_next_to(GTK_GRID(grid_numbers), comma, zero, GTK_POS_RIGHT,10, 10);
mod = gtk_button_new_with_label("%");
gtk_grid_attach_next_to(GTK_GRID(grid_numbers), mod, comma, GTK_POS_RIGHT, 10, 10);
plus = gtk_button_new_with_label("+");
gtk_grid_attach_next_to(GTK_GRID(grid_numbers), plus, mod, GTK_POS_RIGHT, 10, 10);
equal = gtk_button_new_with_label("=");
gtk_grid_attach_next_to(GTK_GRID(grid_numbers), equal, plus, GTK_POS_RIGHT, 10, 20);
one = gtk_button_new_with_label("1");
gtk_grid_attach_next_to(GTK_GRID(grid_numbers), one, zero, GTK_POS_TOP, 10, 10);
gtk_window_present (GTK_WINDOW (window));
}
2条答案
按热度按时间ars1skjm1#
首先,有一个关于你 * 似乎 * 正在做的假设的评论:
gtk_grid_attach()
的最后两个参数width
和height
不是以像素为单位的宽度和高度或类似的东西。它们表示子部件应该采用的列数(或行数)。换句话说,您可能希望在示例的值中将10替换为1,将20替换为2。现在,您有两种选择来实现您想要的:
1.直接使用
gtk_grid_attach()
gtk_grid_attach()
允许您精确地指定元素应该放在哪一行和哪一列1.使用
gtk_grid_attach_next_to()
gtk_grid_attach_next_to()
与GTK_POS_RIGHT
将在内部调用gtk_grid_attach()
,其行号与引用的元素相同。在您的示例中,如果您真的想使用此方法,您可能会在“+”按钮上方的按钮上使用它(我假设是“-”按钮)hsgswve42#
看文档。似乎你不能直接添加一个不可见的孩子,即使高度= 0。检查How to make a GtkButton fully invisible?如果你只想比其他按钮大一个按钮,