我正在尝试一个简单的示例 Gtk 程序并使用 GtkTable 来对齐我的小部件。
我的布局应该是:
窗口背景为黑色。按钮应为白色,而按钮上的文本应为黑色。所有标签都应为白色,以便在黑色窗口背景下可以看到它们。
考虑到这一点。这是我到目前为止所做的。
代码:
#include <gtk/gtk.h>
#include <glib.h>
#include <stdlib.h> /* for atoi() and exit() */
#include <stdio.h> /* standard in and output*/
typedef struct cmd_widgets{
GtkWidget *button1;
GtkWidget *combo;
GtkWidget *label;
}my_cmd_widgets;
static gboolean close_application( GtkWidget *widget, GdkEvent *event, gpointer data )
{
gtk_main_quit ();
return FALSE;
}
static void UpdateChoice( GtkWidget *widget, gpointer data)
{
my_cmd_widgets *widgrp;
widgrp = (my_cmd_widgets *)data;
gchar *text = gtk_combo_box_get_active_text(GTK_COMBO_BOX(widgrp->combo));
GString *val = g_string_new("You have chosen : ");
g_string_append(val, text);
gtk_label_set_text(GTK_LABEL(widgrp->label), val->str);
g_free(text);
}
int main(int argc, char *argv[]) {
GtkWidget *window;
GtkWidget *table;
GtkWidget *label1;
GtkWidget *label2;
GtkStyle *style;
GtkWidget *align;
my_cmd_widgets grp_widgets;
GtkWidget *image;
gtk_init(&argc, &argv);
gtk_rc_parse("fonts.rc");
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_title(GTK_WINDOW(window), "Tour App");
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
table = gtk_table_new(7, 1, FALSE);
gtk_container_add(GTK_CONTAINER(window), table);
label1 = gtk_label_new("Our Tour Package Offers");
align = gtk_alignment_new(0.0, 0.5, 0.0, 0.0);
gtk_container_add(GTK_CONTAINER(align), label1);
gtk_table_attach(GTK_TABLE(table), align, 0, 1, 1, 2, GTK_FILL, GTK_FILL, 0, 10);
label2 = gtk_label_new("Select City: ");
align = gtk_alignment_new(0.0, 0.5, 0.0, 0.0);
gtk_container_add(GTK_CONTAINER(align), label2);
gtk_table_attach(GTK_TABLE(table), align,0, 1, 2, 3, GTK_FILL, GTK_FILL, 0, 0);
gtk_widget_show(align);
grp_widgets.combo = gtk_combo_box_new_text();
gtk_combo_box_append_text(GTK_COMBO_BOX(grp_widgets.combo), "Paris");
gtk_combo_box_append_text(GTK_COMBO_BOX(grp_widgets.combo), "London");
gtk_combo_box_append_text(GTK_COMBO_BOX(grp_widgets.combo), "Tokyo");
gtk_combo_box_append_text(GTK_COMBO_BOX(grp_widgets.combo), "New York");
/* Creates a new button1. */
grp_widgets.button1 = gtk_button_new_with_label ("See Itinerary");
grp_widgets.label = gtk_label_new("");
align = gtk_alignment_new(0.0, 0.5, 0.0, 0.0);
gtk_container_add(GTK_CONTAINER(align), grp_widgets.label);
gtk_table_attach(GTK_TABLE(table), align,0, 1, 5, 6, GTK_FILL, GTK_FILL, 0, 0);
gtk_widget_show(align);
image = gtk_image_new ();
gtk_image_set_from_file (GTK_IMAGE(image), "Images/olympics_logo.gif");
//column 1
gtk_table_attach(GTK_TABLE(table), image, 0, 1, 0, 1,
GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 0, 0);
gtk_table_attach(GTK_TABLE(table), grp_widgets.combo, 0, 1, 3, 4,
GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 0, 15);
gtk_table_attach(GTK_TABLE(table), grp_widgets.button1, 0, 1, 4, 5,
GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 5, 15);
g_signal_connect(window, "destroy",G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect (window,"delete-event",G_CALLBACK (close_application), NULL);
g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), G_OBJECT(window));
g_signal_connect (G_OBJECT(grp_widgets.button1), "clicked", G_CALLBACK (UpdateChoice), (gpointer)&grp_widgets);
gtk_widget_show(table);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
这是包含 TABLE AND WINDOW 属性的 rc 文件:
style "window"
{
font_name = "fixed 12"
bg[NORMAL] = { 0.0, 0.0, 0.0 }
}
style "table"
{
bg[NORMAL] = { 1.0, 1.0, 1.0 }
fg[NORMAL] = { 1.0, 1.0, 1.0 }
}
style "container" {
font_name = "bold 12"
xthickness = 10
ythickness = 10
bg[NORMAL] = { 1.0, 1.0, 1.0 }
fg[NORMAL] = { 1.0, 1.0,1.0 }
}
style "button" {
font_name = "bold 12"
xthickness = 10
ythickness = 10
#bg[NORMAL] = { 0.0, 0.0, 0.0 }
fg[NORMAL] = { 0.0, 0.0, 0.0 }
}
#widget_class "*GtkButton*" style "button"
widget_class "GtkWindow" style "window"
widget_class "*GtkTable*" style "table"
上面代码中的问题是,当我在表格或窗口样式上将 fg 更改为白色时,Button 的文本(标签)仍然是白色的。即使将 fg 更改为黑色(如下所示),我的按钮上的标签文本仍然是白色的。另外,如何拉伸图像和窗口上的所有小部件,以便每当我调整窗口大小时,所有小部件的大小也会调整为适合屏幕?