Hi all,
I would like to change the color of a button in Gtk based on an event and change the color or darken the button when it is selected.
Right now I am stuck simply trying to change the color. My approach is based on this source and this source. I have two files:
example.jl
using Gtk, Gtk.ShortNames
style_file = joinpath(dirname(Base.source_path()), "style.css")
provider = CssProviderLeaf(filename = style_file)
w = Window("")
button = GtkButton("")
set_gtk_property!(button, :name, "green_button")
push!(w, button)
screen = Gtk.GAccessor.style_context(button)
push!(screen, StyleProvider(provider), 600)
Gtk.showall(w)
style.css
#green_button{
background-color: green;
color: white;
font-family: DejaVu Sans;
font-style: normal;
font-weight: bold;
font-size: 20px;
border-radius: 15px;
}
#green_button:checked{
background-color: rgb(152, 226, 152);
color: white;
font-family: DejaVu Sans;
font-style: normal;
font-weight: bold;
font-size: 20px;
border-radius: 15px;
}
Unfortunately, the color does not change. Any help would be appreciated.
1 Like
The following works for labels:
Summary
using Gtk, Gtk.ShortNames
win = Window("Test")
label = Label("Some text")
sc = Gtk.GAccessor.style_context(label)
pr = CssProviderLeaf(data="#blue_text {color:blue;}")
push!(sc, StyleProvider(pr), 600)
set_gtk_property!(label, :name, "blue_text")
push!(win, label)
showall(win)
However, it does not work when adapted for buttons:
Summary
using Gtk, Gtk.ShortNames
w = Window("")
button = GtkButton("")
sc = Gtk.GAccessor.style_context(button)
pr = CssProviderLeaf(data="#green_button {color:green;}")
push!(sc, StyleProvider(pr), 600)
set_gtk_property!(button, :name, "green_button")
push!(w, button)
Gtk.showall(w)
Any ideas?
@Christopher_Fisher: in the css-provider’s data the color:green
should be background:green
@everyone: for those who are interested:
MWE
using Gtk
let
win = GtkWindow("ButtonColorTest 1")
butt = GtkButton("")
sc = GAccessor.style_context(butt)
blue = GtkCssProvider(data="#blue {background:blue;}")
yellow = GtkCssProvider(data="#yellow {background:yellow;}")
spb = GtkStyleProvider(blue)
spy = GtkStyleProvider(yellow)
push!(sc, spb, 600)
set_gtk_property!(butt, :name, "blue")
clickhandler = signal_connect(butt, "clicked") do widget
if get_gtk_property(widget, :name, String) == "blue"
push!(sc, spy, 600)
set_gtk_property!(butt, :name, "yellow")
else
push!(sc, spb, 600)
set_gtk_property!(butt, :name, "blue")
end
end
push!(win, butt)
showall(win)
end
1 Like