2

I am trying to change the color of text in a label on the fly at runtime. I've tried applying a css style, I've tried two depreciated methods, and none of it works. Can it even be done, and if not, why is something this simple not available?

Applying a css style on the fly partially works: when I specify

.pinkStyle {
    background-color: rgb(241, 135, 135);
    color: black;
}

at runtime I can see the background turn pink. But the text stays white.

3

Oh my gosh. I'm documenting this so no one will suffer the way I suffered.

If you want runtime control of your text, do not under any circumstances use Glade to set the foreground color with Edit Attributes. If you do, you have PERMANENTLY set the text color in a way that neither css changes, pango markup, or depreciated functions like gtk_widget_modify_fg can touch at runtime.

You can still use css to change the background color of the label, but to get at the text's own color and background, I'm using gtk_label_set_markup with

<span background=\"#0022ff\" foreground=\"#ff0044\">

with success. AFTER deleting all attributes from all my labels in Glade.

GTK is a nightmare; I've never met anything in Linux before that made me long for Windows, but this did it.

| improve this answer | |
1

GTK is ugly, partly because of all the deprecated stuff. They didn't fix problems, they patched around them.

Anyway, just got this going, it's also on raspberrypi.org forums. err is an int, btcprice and oldprice are floats, markup and errstr are gchar.

if (err == 0) {
  if (btcprice > oldprice)
    markup = g_strdup_printf("<span foreground='green'>%.2f</span>",btcprice);
  else
    markup = g_strdup_printf("<span foreground='red'>%.2f</span>",btcprice);
 } else {
   markup = g_strdup_printf("<span foreground='orange'>%s</span>",errstr);
 }
 gtk_label_set_markup(GTK_LABEL(pLabel),markup);

Text color is red if the price is falling, green if it's rising, orange if there's an error. g_strdup_printf() is a little like printf or snprintf but it gets a float into a string whose color changes depending on the value of the float.

| improve this answer | |
  • "GTK is ugly, partly because of all the deprecated stuff. They didn't fix problems, they patched around them." <- +1 for that. It's a mess. – Jacob Vlijm Feb 21 '19 at 11:05
0

Here's a variation on the above code that is a callback function which responds to a click on the label (a button_press_event signal) and changes label text and color based on the label's current text. It runs fine on Raspberry Pi. Thanks, guys.

void on_block_01_pwr_button_press_event()
{   GtkWidget *label=GTK_WIDGET(block_01_pwr);
    const gchar * txt;
    char *format;
    gchar *markup;
    txt=gtk_label_get_text((GtkLabel *) block_01_pwr);
    int x=strcmp("pwr #1", txt);
    if(x==0)
    {   txt="pwr #2";
        format="<span foreground=\"#40c0c0\">%s</span>";
        markup=g_markup_printf_escaped(format,txt);
    }
    else 
    {   x=strcmp("pwr #2", txt);
        if(x==0){txt="pwr off";
        format="<span foreground=\"#999999\">%s</span>";
        markup=g_markup_printf_escaped(format,txt);
                }
        else 
        {txt="pwr #1";
    format="<span foreground=\"#000000\">%s</span>";
    markup=g_markup_printf_escaped(format,txt);
        }
    }
    gtk_label_set_text((GtkLabel *) block_01_pwr, txt);
    gtk_label_set_markup(GTK_LABEL (label), markup);
    g_free(markup);
// Add code here to set this block's power pack # (or off) and data array entry
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.