更改GtkWindow时更改GtkButton颜色

时间:2012-05-23 15:05:01

标签: c gtk

我正在尝试一个简单的示例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更改为黑色(如下所示),我的按钮上的标签文本仍为白色。另外,我如何在窗口上拉伸图像和所有小部件,这样每当我调整窗口大小时,所有小部件都会调整大小以适应屏幕?

1 个答案:

答案 0 :(得分:1)

  

所有标签都应该是白色的,以便可以在黑色窗口背景下看到它们。

根据我在网上发现的内容:

  

GtkLabel Widget是少数几个不创建它们的GTK +小部件之一   拥有自己的窗口。这个menas为了设定   gtkLabel Widget的bg颜色,你需要改变它的bg颜色   父节点。

所以在这种情况下,举个简单的例子,我只在GtkEventBox中打包了一个标签。
代码片:

GtkWidget *eventBox;
...
eventBox = gtk_event_box_new();

label1 = gtk_label_new("Our Tour Package Offers");
gtk_container_add(GTK_CONTAINER(eventBox), label1);
align = gtk_alignment_new(0.0, 0.5, 0.0, 0.0);
gtk_container_add(GTK_CONTAINER(align), eventBox);
...

简化的rc文件:

style "window"
{
  font_name = "fixed 12"
  bg[NORMAL] = { 0.0, 0.0, 0.0 }
}

style "label"{
  bg[NORMAL] = { 1.0, 1.0, 1.0 }
}

widget_class "GtkWindow" style "window"
widget_class "*GtkAlignment*GtkEventBox*" style "label"

和结果:
enter image description here