Search code examples
rubyuser-interfaceraspberry-pigtkgtk3

Two colors in one string


Hi I'm trying to do a GUI code using GTK in Ruby and I'm stuck trying to change the color of a String.

I would like the Welcome to be blue and the @name to be red but I can't seem to figure out a way to get both of them

@user = Gtk::Label.new("Welcome #{@name}")
css_user = Gtk::CssProvider.new
css_user.load(data: "label{color: blue;}")

If anybody could help I would be really greatful


Solution

  • I had to change a little bit the gtk display

    @box = Gtk::Box.new(:horizontal, 1)
    @welcome = Gtk::Label.new("Welcome ")
    @user = Gtk::Label.new(@usuari)
    css_user = Gtk::CssProvider.new
    css_user.load(data: "label{color: blue;}")
    css_welcome = Gtk::CssProvider.new
    css_welcome.load(data: "label{color: black;}")
    @user.style_context.add_provider(css_user, Gtk::StyleProvider::PRIORITY_USER)
    @welcome.style_context.add_provider(css_welcome, Gtk::StyleProvider::PRIORITY_USER)
    

    As you can see I created two labels (one for each color) and I placed them inside a Horizontal Box