DevHeads.net

Ruby GTK modify widget colors

Hello,

I want to modify the widget colors in GTK3. I only found an example with
GTK2.

With GTK3 it doesn't work.

Has someone a solution?

Regards

UH

require 'gtk2'

window= Gtk::Window.new
window.set_size_request(300,200)

button= Gtk::Button.new('Button')
button.set_size_request(100,-1)

font= Pango::FontDescription.new('28')
button.child.modify_font(font)

yellow= Gdk::Color.new(65535,65535,0)
cyan= Gdk::Color.new(0,65535,65535)
green= Gdk::Color.new(0,65535,0)
magenta= Gdk::Color.new(65535,0,65535)
white= Gdk::Color.new(65535,65535,65535)
blue= Gdk::Color.new(0,0,65535)

# background color of the button
button.modify_bg(Gtk::STATE_NORMAL , yellow)
button.modify_bg(Gtk::STATE_PRELIGHT, cyan)
button.modify_bg(Gtk::STATE_ACTIVE , green)

# label color of the button
button.child.modify_fg(Gtk::STATE_NORMAL , magenta)
button.child.modify_fg(Gtk::STATE_PRELIGHT, blue)
button.child.modify_fg(Gtk::STATE_ACTIVE , white)

fixed= Gtk::Fixed.new
fixed.put(button,10,10)
window.add(fixed)

window.show_all
window.signal_connect("destroy") {Gtk.main_quit }
Gtk.main

Comments

Re: Ruby GTK modify widget colors

By Kouhei Sutou at 01/12/2019 - 02:26

Hi,

In <f339fabb-6ed1-6830-ffe0- ... at unitybox dot de>
"Ruby GTK modify widget colors" on Thu, 10 Jan 2019 23:38:55 +0100,

You can use CSS in GTK+ 3:

require "gtk3"

application = Gtk::Application.new
application.signal_connect(:activate) do
provider = Gtk::CssProvider.new
provider.load(data: <<-CSS)
button {
background-image: none;
background-color: yellow;
}

label {
font-size: 28px;
color: magenta;
}

button:hover {
background-color: cyan;
}

button:hover label {
color: blue;
}

button:active {
background-color: green;
}

button:active label {
color: white;
}
CSS
Gtk::StyleContext.add_provider_for_screen(Gdk::Screen.default,
provider,
Gtk::StyleProvider::PRIORITY_APPLICATION)

window = Gtk::ApplicationWindow.new(application)
window.set_size_request(300, 200)

button = Gtk::Button.new(label: "Button")
button.set_size_request(100, -1)

fixed = Gtk::Fixed.new
fixed.put(button, 10, 10)

window << fixed
window.show_all
end
application.run

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>