gpt4 book ai didi

c - gtk 3.4 调整小部件的大小和附加到条目小部件的标签

转载 作者:太空宇宙 更新时间:2023-11-04 02:13:51 24 4
gpt4 key购买 nike

我正在尝试为我 future 的项目(客户端 FTP)提供 GTK+3 图形用户界面,但我遇到了一些问题。这是我的第一个代码:

#include <gtk/gtk.h>

/* When "connect" button is clicked a message will appear */
static void print_hello (GtkWidget *widget, gpointer data)
{
g_print ("Connect button clicked\n");
}

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

/* Declare widgets */
GtkWidget *window;
GtkWidget *grid;
GtkWidget *button;
GtkWidget *u_name;
GtkWidget *h_name;
GtkWidget *pass;
GtkWidget *label_user;
GtkWidget *label_host;
GtkWidget *label_pass;

/*This is called in all GTK applications. Arguments are parsed from the command line and are returned to the application.*/
gtk_init (&argc, &argv);

/* create a new window, set its title and put it on center */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "FTP Client");
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 20);
//gtk_window_set_resizable(GTK_WINDOW(window), FALSE); /* window is NOT resizable */

/* Here we construct the container that is going pack our buttons */
grid = gtk_grid_new ();

/* Pack the container in the window */
gtk_container_add (GTK_CONTAINER (window), grid);

/* Add labels */
label_user = gtk_label_new("Username ");
label_host = gtk_label_new("Hostname ");
label_pass = gtk_label_new("Password ");

/* Add hostname entry and label */
h_name = gtk_entry_new();
gtk_grid_attach (GTK_GRID (grid), label_host, 0, 0, 1, 1);
gtk_grid_attach (GTK_GRID (grid), h_name, 1, 0, 1, 1);

/* Add username entry and label */
u_name = gtk_entry_new();
gtk_grid_attach (GTK_GRID (grid), label_user, 0, 1, 1, 1);
gtk_grid_attach (GTK_GRID (grid), u_name, 1, 1, 2, 1);

/* Add password entry and label (visibility set to 0 = not visible */
pass = gtk_entry_new();
gtk_grid_attach (GTK_GRID (grid), label_pass, 0, 2, 1, 1);
gtk_entry_set_visibility (GTK_ENTRY (pass), 0);
gtk_grid_attach (GTK_GRID (grid), pass, 1, 2, 1, 1);

/* Add connect button */
button = gtk_button_new_with_label ("Connect");
g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
gtk_grid_attach (GTK_GRID (grid), button, 0, 3, 2, 1);

/* Add quit button */
button = gtk_button_new_with_label ("Quit");
g_signal_connect (button, "clicked", G_CALLBACK (gtk_main_quit), NULL);
gtk_grid_attach (GTK_GRID (grid), button, 0, 4, 2, 1);

/* Now that we are done packing our widgets, we show them all in one go, by calling gtk_widget_show_all() on the window.*/
gtk_widget_show_all (window);

/* All GTK applications must have a gtk_main(). Control ends here and waits for an event to occur (like a key press or a mouse event),
* until gtk_main_quit() is called. */
gtk_main ();

return 0;
}

该代码中的问题是,如果要调整窗口大小时,我无法调整小部件的大小。我认为这是一个 gtk_grid 问题,所以我无法理解如何使“小部件适合窗口”。我该怎么办?

PS:我更喜欢使用 gtk_grid,因为在 GTK+3.4 中,gtk_table 已被弃用

最佳答案

在您要展开的小部件上将“heexpand”和/或“vexpand”属性设置为TRUE。例如,添加此代码,您将看到 u_name 填充可用空间:

gtk_widget_set_hexpand(u_name, TRUE);
gtk_widget_set_vexpand(u_name, TRUE);

关于c - gtk 3.4 调整小部件的大小和附加到条目小部件的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10257563/

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