Anunţ

Caută printre mesajele de pe forum înainte de a scrie unul nou!
Formulează corect întrebarea sau problema pe care o ai.
Respectă regulile forumului și Codul de Conduită!

#1 22 Jan 2017 20:51:53

TARA24.
Membru
Înregistrat: 23 Dec 2010
Mesaje: 961

[REZOLVAT]Schimbare Culoare button in GTK3

Salutare tutor,

Am nevoie de cineva care se pricepe in GTK3, deoarece nu reusesc sa schimb culoarea unui buton,.

Aplicatia arata cam asa:
http://i.imgur.com/9qc7K8P.png

Si dupa cum se observa am un Buton acolo ( my button) care doresc sa ii schimb culoarea.

Din pacate functiile din GTK2 nu functioneaza iar pe net am gasit ca ar fi facuta din CSS, ei bine eu nu reusesc.

Aici este sursa:

Cod:

#include <gtk/gtk.h>

void myStyleCss(void);
void createWind(GtkWidget **window, gint width, gint height);
void createGrid(GtkWidget **grid, GtkWidget **window, const gchar *name);
void createGridChild(GtkWidget **child, GtkWidget **grid, const gchar *first_property_name,
                                gint set_width_size, gint set_height_Size, gint margin,
                                gint left, gint top, gint width_col_span, gint height_col_span);

void createButton_with_label(GtkWidget **button, GtkWidget **gid, const gchar *first_property_name,
                                        gint margin, gint set_width_size, gint set_height_Size,
                                        gint left, gint top, gint width_col_span, gint height_col_span);

gint giContador = 0;
char buf[5];

int main(int argc, char *argv[]){
    GtkWidget *window;
    GtkWidget *grid;
    GtkWidget *child;
    GtkWidget *button;


    gtk_init(&argc, &argv);
    myStyleCss();
    createWind(&window, 450, 200);
    createGrid(&grid, &window, "myGrid");
    createGridChild ( &child, &grid, "myChild", 100, 100, 40, 0, 0, 1, 1 );

    button = gtk_button_new_with_label("My Button");
    createButton_with_label(&button, &child, "myButton", 25, 115, 115, 0, 0, 1, 1);

    gtk_widget_show_all(window);
    gtk_main();
}

void myStyleCss(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), TRUE);
    gtk_grid_set_column_homogeneous(GTK_GRID(*grid), TRUE);
    gtk_widget_set_name(*grid, name);
    gtk_container_set_border_width(GTK_CONTAINER (*grid), 0);
    g_object_set (*grid, "margin", 22, NULL);
    gtk_container_add (GTK_CONTAINER (*window), *grid);
}

void createGridChild(GtkWidget **child, GtkWidget **grid, const gchar *name,
                                gint set_width_size, gint set_height_size, gint margin,
                                gint left, gint top, gint width_col_span, gint height_col_span)
{
    *child = gtk_grid_new ();
    gtk_widget_set_name(*child, name);
    gtk_widget_set_size_request(*child, set_width_size, set_height_size);
    g_object_set (*child, "margin", margin, NULL);
    gtk_grid_attach (GTK_GRID (*grid), *child, left, top, width_col_span, height_col_span);
}

void createButton_with_label(GtkWidget **button, GtkWidget **gid, const gchar *first_property_name,
                                        gint margin, gint set_width_size, gint set_height_Size,
                                        gint left, gint top, gint width_col_span, gint height_col_span)
{
    gtk_widget_set_name(*button, first_property_name);

    g_object_set (*button, "margin", margin, NULL);
    gtk_widget_set_size_request(*button, set_width_size, set_height_Size);
    gtk_grid_attach(GTK_GRID(*gid), *button,    left, top, width_col_span, height_col_span);
}

Aici este fisierul CSS (mystyle.css):

Cod:

GtkWindow {
    background-color: magenta;
    color: cyan;
    border-width: 3px;
    border-color: blue;
}

#myGrid {
    background-color: red;
    border-style: solid;
    border-color: black;
    border-width: 3px;
    border-radius: 15px;
    border-color: grey;
}

#myChild {
    background-color: white;
    border-style: solid;
    border-color: black;
    border-width: 3px;
    border-radius: 15px;
    border-color: grey;
}

#myButton {
    color: black;
    background-color: black;
    font: Comic Sans 15;
}



GtkButton {
    background-color: black;
    color: black;
    border-style: solid;
    border-radius: 15px;
    border-width: 3px;
    border-color: blue;
}

Din pacate nici cu #myButton si nici cu GtkButton nu se intampla nimic,

Stie cineva ce e de facut? Repet GTK3, mai exact:

tara24@tara24-HP-Pavilion-17-Notebook-PC ~ $ pkg-config --modversion gtk+-3.0
3.18.9

L.E:

Am reusit ceva, daca adaug in CSS:

Cod:

GtkButton {
    background-image: none;
    background-color: blue;
}

GtkButton:active {
    background-color: #00FF00;
}

GtkButton:hover:active {
    background-image: none; 
    background-color: #00FF00;
}

Dar nu inteleg de ce nu merge cu:

Cod:

#myButton{
    background-color: red;
    color: red;
}

Am rezolvat adaugand in CSS urmaotarele:

Cod:

GtkButton:checked{
    background-color: #00FF00;
}

Editat ultima oară de TARA24. (23 Jan 2017 00:16:22)


Orice decizie pe care esti pe cale sa o iei, gandeste-o de cel putin 3 ori.
Din momentul in care ai luat-o nu mai conteaza de cate ori ai gandit-o.
Fi destept, foloseste ce ti se potriveste nu ce ti se impune.

Offline

 
Feed

Antet forum

Powered by FluxBB