我目前正在使用gtk库,尝试使用C创建一个简单的窗口应用程序,但是我在表格中对齐标签时遇到了一些困难。
更具体地说,这是我到目前为止所写的内容:
#include<gtk/gtk.h>
static void dest(GtkWidget*, gpointer);
int main(int argc, char *argv[]) {
GtkWidget *window;
GtkWidget *table;
GtkWidget *label1, *folderChooser1;
GtkWidget *label2, *folderChooser2;
gtk_init(&argc,&argv);
/*************************************
* setting the first row in table
*************************************/
label1=gtk_label_new("Browse For a Folder To Backup: ");
//gtk_misc_set_alignment(GTK_MISC(label1), 0, .5); // neither this
//gtk_label_set_justify(GTK_LABEL(label1, GTK_JUSTIFY_LEFT); // nor this aligns the label to the left.
folderChooser1=gtk_file_chooser_button_new("Browse For Folder...", GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER (folderChooser1), g_get_home_dir());
/*************************************
* setting the second row
*************************************/
label2=gtk_label_new("Choose Backup Path: ");
//gtk_misc_set_alignment(GTK_MISC(label2), 0, .5); // neither this
//gtk_label_set_justify(GTK_LABEL(label2, GTK_JUSTIFY_LEFT); // nor this aligns the label to the left.
folderChooser2=gtk_file_chooser_button_new("Browse For Folder...", GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER (folderChooser2), g_get_home_dir());
/***************************************
* setting table
***************************************/
table=gtk_table_new(2,2,TRUE);
gtk_table_attach(GTK_TABLE(table), label1, 0, 1, 0, 1, GTK_SHRINK, GTK_SHRINK, 15, 0);
gtk_table_attach(GTK_TABLE(table), label2, 0, 1, 1, 2, GTK_SHRINK, GTK_SHRINK, 15, 0);
gtk_table_attach_defaults(GTK_TABLE(table), folderChooser1, 1, 2, 0, 1);
gtk_table_attach_defaults(GTK_TABLE(table), folderChooser2, 1, 2, 1, 2);
/********************************
* setting window
********************************/
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Backup");
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(dest), NULL);
gtk_container_add(GTK_CONTAINER (window), table);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
static void dest(GtkWidget *window, gpointer data) {
gtk_main_quit();
}
上面的代码将产生这个:
正如您所看到的,标签位于中心位置,我想要将其标记为左侧。 没有一条注释掉的线路对我有用 有没有办法在表格单元格内对齐标签?
答案 0 :(得分:1)
gtk_misc_set_alignment
附加标签, gtk_table_attach_defaults
对我有用:
完整的参考代码:
#include<gtk/gtk.h>
static void dest(GtkWidget*, gpointer);
int main(int argc, char *argv[]) {
GtkWidget *window;
GtkWidget *table;
GtkWidget *label1, *folderChooser1;
GtkWidget *label2, *folderChooser2;
gtk_init(&argc,&argv);
/*************************************
* setting the first row in table
*************************************/
label1=gtk_label_new("Browse For a Folder To Backup: ");
gtk_misc_set_alignment(GTK_MISC(label1), 0, .5); // neither this
//gtk_label_set_justify(GTK_LABEL(label1, GTK_JUSTIFY_LEFT); // nor this aligns the label to the left.
folderChooser1=gtk_file_chooser_button_new("Browse For Folder...", GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER (folderChooser1), g_get_home_dir());
/*************************************
* setting the second row
*************************************/
label2=gtk_label_new("Choose Backup Path: ");
gtk_misc_set_alignment(GTK_MISC(label2), 0, .5); // neither this
//gtk_label_set_justify(GTK_LABEL(label2, GTK_JUSTIFY_LEFT); // nor this aligns the label to the left.
folderChooser2=gtk_file_chooser_button_new("Browse For Folder...", GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER (folderChooser2), g_get_home_dir());
/***************************************
* setting table
***************************************/
table=gtk_table_new(2,2,TRUE);
//gtk_table_attach(GTK_TABLE(table), label1, 0, 1, 0, 1, GTK_SHRINK, GTK_SHRINK, 15, 0);
//gtk_table_attach(GTK_TABLE(table), label2, 0, 1, 1, 2, GTK_SHRINK, GTK_SHRINK, 15, 0);
gtk_table_attach_defaults(GTK_TABLE(table), label1, 0, 1, 0, 1);
gtk_table_attach_defaults(GTK_TABLE(table), label2, 0, 1, 1, 2);
gtk_table_attach_defaults(GTK_TABLE(table), folderChooser1, 1, 2, 0, 1);
gtk_table_attach_defaults(GTK_TABLE(table), folderChooser2, 1, 2, 1, 2);
/********************************
* setting window
********************************/
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Backup");
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(dest), NULL);
gtk_container_add(GTK_CONTAINER (window), table);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
static void dest(GtkWidget *window, gpointer data) {
gtk_main_quit();
}
(在Ubuntu 12.04上使用gtk 2.0测试)