我设法使用CSS更改了Button(在本例中为切换按钮)的背景,但我无法弄清楚如何让按下的按钮保持其颜色。
现在按钮的颜色为蓝色,如果单击并按住按钮,按钮将变为绿色。
但我需要保持按钮的颜色为绿色,直到我再次按下按钮再次变为蓝色。
以下是代码:
#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 chngBkgrd(gpointer *data);
int main(int argc, char *argv[]){
GtkWidget *window;
GtkWidget *grid;
GtkWidget *button;
gtk_init(&argc, &argv);
myStyleCss();
createWind(&window, 350, 250);
createGrid(&grid, &window, "myGrid");
button = gtk_toggle_button_new_with_label("tgl_btn");
g_signal_connect (button, "toggled", G_CALLBACK(chngBkgrd),(gpointer *)button);
gtk_grid_attach (GTK_GRID (grid), button, 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), 50);
g_object_set (*grid, "margin", 0, NULL);
gtk_container_add (GTK_CONTAINER (*window), *grid);
}
void chngBkgrd(gpointer *data){
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(data))){
g_print("On\n");
}else{
g_print("Off\n");
}
}
这是CSS文件(mystyle.css):
GtkWindow {
background-color: #A4A4A4;
color: cyan;
border-width: 3px;
border-color: blue;
}
#myGrid {
background-color: white;
border-style: solid;
border-width: 3px;
border-radius: 15px;
border-color: grey;
}
#myChild {
background-color: red;
border-style: solid;
border-width: 3px;
border-radius: 15px;
border-color: grey;
}
#tgl_btn{
background-color: red;
color: red;
}
GtkButton {
background-image: none;
background-color: blue;
}
GtkButton:active {
background-color: #00FF00;
}
我该怎么做?
答案 0 :(得分:1)
在我读完以下内容之后,我设法弄明白了怎么做
解决方案是使用:已检查:
GtkButton:checked{
background-color: #00FF00;
}