GTK修改GtkButton的背景颜色

发布于 2024-08-10 03:58:34 字数 488 浏览 5 评论 0原文

尝试更改背景/前景色....使用 Gtk+ 和 C。

  GdkColor color;
  gdk_color_parse( "#0080FF", &color );
  gtk_widget_modify_fg( GTK_WIDGET(button), GTK_STATE_SELECTED, &color );
  gtk_widget_modify_fg( GTK_WIDGET(button), GTK_STATE_NORMAL, &color );

我正在使用上述功能,但它没有给出任何结果。

我正在寻找这个问题中提出的问题(但是在 C 中):
GTK:创建彩色常规按钮

如何使用按钮样式更改按钮的样式? 有人可以提供一些例子吗?

Trying to change background/foreground color....Using Gtk+ and C.

  GdkColor color;
  gdk_color_parse( "#0080FF", &color );
  gtk_widget_modify_fg( GTK_WIDGET(button), GTK_STATE_SELECTED, &color );
  gtk_widget_modify_fg( GTK_WIDGET(button), GTK_STATE_NORMAL, &color );

I am using above functionality but it is not giving any results.

I am looking for something asked in this question(But in C):
GTK: create a colored regular button

how to Change Style of Button using button Style?
Can anybody provide some examples?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半山落雨半山空 2024-08-17 03:58:34

GTK3 中,您可以使用 CSS

在此处输入图像描述

如下所示:

#include <gtk/gtk.h>
#include <string.h>

void btn_clicked(GtkWidget *widget, GtkEntry *entry);
void myCSS(void);
void createWind(GtkWidget **window, gint width, gint height);
void createGrid(GtkWidget **grid, GtkWidget **window, const gchar *name);

int main(int argc, char *argv[]){
    GtkWidget *window, *grid;
    GtkWidget *button1, *button2, *button3, *button4;

    gtk_init(&argc, &argv);
    myCSS();

    /*     Create the Window     */
    createWind(&window, 390, 290);

    /*     Create a Grid     */
    createGrid(&grid, &window, "myGrid");


    /*     Create a red Button    */
    button1 = gtk_button_new_with_label("Red");
    gtk_widget_set_name(button1, "myButton_red");
    gtk_widget_set_size_request(button1, 160, 130);
    g_object_set (button1, "margin", 5, NULL);

    /*     Create a yellow Button    */
    button2 = gtk_button_new_with_label("Yellow");
    gtk_widget_set_name(button2, "myButton_yellow");
    gtk_widget_set_size_request(button2, 160, 130);
    g_object_set (button2, "margin", 5, NULL);

    /*     Create a green Button    */
    button3 = gtk_button_new_with_label("Green");
    gtk_widget_set_name(button3, "myButton_green");
    gtk_widget_set_size_request(button3, 160, 130);
    g_object_set (button3, "margin", 5, NULL);

    /*     Create a blue Button    */
    button4 = gtk_button_new_with_label("Blue");
    gtk_widget_set_name(button4, "myButton_blue");
    gtk_widget_set_size_request(button4, 160, 130);
    g_object_set (button4, "margin", 5, NULL);


    /*     Putting all together      */
    gtk_grid_attach(GTK_GRID(grid), button1, 0, 0, 1, 1);
    gtk_grid_attach(GTK_GRID(grid), button2, 1, 0, 1, 1);
    gtk_grid_attach(GTK_GRID(grid), button3, 0, 1, 1, 1);
    gtk_grid_attach(GTK_GRID(grid), button4, 1, 1, 1, 1);



    gtk_widget_show_all(window);
    gtk_main();
    return 0;
}

void myCSS(void){
    GtkCssProvider *provider;
    GdkDisplay *display;
    GdkScreen *screen;

    provider = gtk_css_provider_new ();
    display = gdk_display_get_default ();
    screen = gdk_display_get_default_screen (display);
    gtk_style_context_add_provider_for_screen (screen, GTK_STYLE_PROVIDER (provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

    const gchar *myCssFile = "mystyle.css";
    GError *error = 0;

    gtk_css_provider_load_from_file(provider, g_file_new_for_path(myCssFile), &error);
    g_object_unref (provider);
}

void createWind(GtkWidget **window, gint width, gint height){
    *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(*window), "MyApp");
    gtk_window_set_default_size(GTK_WINDOW(*window), width, height);
    gtk_window_set_resizable (GTK_WINDOW(*window), TRUE);
    gtk_container_set_border_width(GTK_CONTAINER(*window), 5);
    g_signal_connect(*window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
}

void createGrid(GtkWidget **grid, GtkWidget **window, const gchar *name){
    *grid = gtk_grid_new ();
    gtk_grid_set_row_homogeneous(GTK_GRID(*grid), FALSE);
    gtk_grid_set_column_homogeneous(GTK_GRID(*grid), FALSE);
    gtk_container_set_border_width(GTK_CONTAINER (*grid), 15);
    gtk_grid_set_column_spacing(GTK_GRID(*grid), 5);
    gtk_grid_set_row_spacing(GTK_GRID(*grid), 5);
    gtk_widget_set_name(*grid, name);
    g_object_set (*grid, "margin", 22, NULL);
    gtk_container_add (GTK_CONTAINER (*window), *grid);
}

void btn_clicked(GtkWidget *widget, GtkEntry *entry){
    (void)widget;
    const gchar *gstrTexto;

    gstrTexto = gtk_entry_get_text(entry);
    g_print("%s\n", gstrTexto);
    gtk_editable_select_region(GTK_EDITABLE(entry) , 0, 3);
}

mystyle.css:

GtkWindow {
    background-color: magenta;
}

GtkButton {
    background-image: none;
}

#myGrid {
    background-color: cyan;
    border-style: solid;
    border-color: black;
    border-width: 1px;
}

#myChildTop {
    background-color: white;
}

#myButton_red{
    background-color: red;
    color: white;
    font-family: DejaVu Sans;
    font-style: normal;
    font-weight: bold;
    font-size: 20px;
    border-radius: 15px;
}

#myButton_yellow{
    background-color: yellow;
    color: white;
    font-family: DejaVu Sans;
    font-style: normal;
    font-weight: bold;
    font-size: 20px;
    border-radius: 15px;
}

#myButton_green{
    background-color: green;
    color: white;
    font-family: DejaVu Sans;
    font-style: normal;
    font-weight: bold;
    font-size: 20px;
    border-radius: 15px;
}

#myButton_blue{
    background-color: blue;
    color: white;
    font-family: DejaVu Sans;
    font-style: normal;
    font-weight: bold;
    font-size: 20px;
    border-radius: 15px;
}

#myButton_red:hover,
#myButton_yellow:hover,
#myButton_green:hover,
#myButton_blue:hover{
   background-color: black;
}

如果将鼠标聚焦在一个按钮上,该按钮将变为黑色:

输入图像描述此处

有关使用GTK3 - CSS- C 语言的更多信息,您可以在此处找到

In GTK3 you do it using CSS

enter image description here

like this:

#include <gtk/gtk.h>
#include <string.h>

void btn_clicked(GtkWidget *widget, GtkEntry *entry);
void myCSS(void);
void createWind(GtkWidget **window, gint width, gint height);
void createGrid(GtkWidget **grid, GtkWidget **window, const gchar *name);

int main(int argc, char *argv[]){
    GtkWidget *window, *grid;
    GtkWidget *button1, *button2, *button3, *button4;

    gtk_init(&argc, &argv);
    myCSS();

    /*     Create the Window     */
    createWind(&window, 390, 290);

    /*     Create a Grid     */
    createGrid(&grid, &window, "myGrid");


    /*     Create a red Button    */
    button1 = gtk_button_new_with_label("Red");
    gtk_widget_set_name(button1, "myButton_red");
    gtk_widget_set_size_request(button1, 160, 130);
    g_object_set (button1, "margin", 5, NULL);

    /*     Create a yellow Button    */
    button2 = gtk_button_new_with_label("Yellow");
    gtk_widget_set_name(button2, "myButton_yellow");
    gtk_widget_set_size_request(button2, 160, 130);
    g_object_set (button2, "margin", 5, NULL);

    /*     Create a green Button    */
    button3 = gtk_button_new_with_label("Green");
    gtk_widget_set_name(button3, "myButton_green");
    gtk_widget_set_size_request(button3, 160, 130);
    g_object_set (button3, "margin", 5, NULL);

    /*     Create a blue Button    */
    button4 = gtk_button_new_with_label("Blue");
    gtk_widget_set_name(button4, "myButton_blue");
    gtk_widget_set_size_request(button4, 160, 130);
    g_object_set (button4, "margin", 5, NULL);


    /*     Putting all together      */
    gtk_grid_attach(GTK_GRID(grid), button1, 0, 0, 1, 1);
    gtk_grid_attach(GTK_GRID(grid), button2, 1, 0, 1, 1);
    gtk_grid_attach(GTK_GRID(grid), button3, 0, 1, 1, 1);
    gtk_grid_attach(GTK_GRID(grid), button4, 1, 1, 1, 1);



    gtk_widget_show_all(window);
    gtk_main();
    return 0;
}

void myCSS(void){
    GtkCssProvider *provider;
    GdkDisplay *display;
    GdkScreen *screen;

    provider = gtk_css_provider_new ();
    display = gdk_display_get_default ();
    screen = gdk_display_get_default_screen (display);
    gtk_style_context_add_provider_for_screen (screen, GTK_STYLE_PROVIDER (provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

    const gchar *myCssFile = "mystyle.css";
    GError *error = 0;

    gtk_css_provider_load_from_file(provider, g_file_new_for_path(myCssFile), &error);
    g_object_unref (provider);
}

void createWind(GtkWidget **window, gint width, gint height){
    *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(*window), "MyApp");
    gtk_window_set_default_size(GTK_WINDOW(*window), width, height);
    gtk_window_set_resizable (GTK_WINDOW(*window), TRUE);
    gtk_container_set_border_width(GTK_CONTAINER(*window), 5);
    g_signal_connect(*window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
}

void createGrid(GtkWidget **grid, GtkWidget **window, const gchar *name){
    *grid = gtk_grid_new ();
    gtk_grid_set_row_homogeneous(GTK_GRID(*grid), FALSE);
    gtk_grid_set_column_homogeneous(GTK_GRID(*grid), FALSE);
    gtk_container_set_border_width(GTK_CONTAINER (*grid), 15);
    gtk_grid_set_column_spacing(GTK_GRID(*grid), 5);
    gtk_grid_set_row_spacing(GTK_GRID(*grid), 5);
    gtk_widget_set_name(*grid, name);
    g_object_set (*grid, "margin", 22, NULL);
    gtk_container_add (GTK_CONTAINER (*window), *grid);
}

void btn_clicked(GtkWidget *widget, GtkEntry *entry){
    (void)widget;
    const gchar *gstrTexto;

    gstrTexto = gtk_entry_get_text(entry);
    g_print("%s\n", gstrTexto);
    gtk_editable_select_region(GTK_EDITABLE(entry) , 0, 3);
}

mystyle.css:

GtkWindow {
    background-color: magenta;
}

GtkButton {
    background-image: none;
}

#myGrid {
    background-color: cyan;
    border-style: solid;
    border-color: black;
    border-width: 1px;
}

#myChildTop {
    background-color: white;
}

#myButton_red{
    background-color: red;
    color: white;
    font-family: DejaVu Sans;
    font-style: normal;
    font-weight: bold;
    font-size: 20px;
    border-radius: 15px;
}

#myButton_yellow{
    background-color: yellow;
    color: white;
    font-family: DejaVu Sans;
    font-style: normal;
    font-weight: bold;
    font-size: 20px;
    border-radius: 15px;
}

#myButton_green{
    background-color: green;
    color: white;
    font-family: DejaVu Sans;
    font-style: normal;
    font-weight: bold;
    font-size: 20px;
    border-radius: 15px;
}

#myButton_blue{
    background-color: blue;
    color: white;
    font-family: DejaVu Sans;
    font-style: normal;
    font-weight: bold;
    font-size: 20px;
    border-radius: 15px;
}

#myButton_red:hover,
#myButton_yellow:hover,
#myButton_green:hover,
#myButton_blue:hover{
   background-color: black;
}

If you focus our mouse on one Button, the Button will become Black:

enter image description here

More about working with GTK3 - CSS- C Language you find Here.

温柔女人霸气范 2024-08-17 03:58:34

红色 GtkButton 的图片 http://www.ubuntu-pics.de/bild/30465 /screenshot_001_1Jt60q.png

GdkColor color;

gdk_color_parse ("red", &color);

gtk_widget_modify_bg ( GTK_WIDGET(button), GTK_STATE_NORMAL, &color);

请注意,我们正在修改 bg 而不是 fg

Picture of Red GtkButton http://www.ubuntu-pics.de/bild/30465/screenshot_001_1Jt60q.png

GdkColor color;

gdk_color_parse ("red", &color);

gtk_widget_modify_bg ( GTK_WIDGET(button), GTK_STATE_NORMAL, &color);

Notice that we are modifying bg instead of fg.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文