GTK+3 change text color in a label (raspberry pi)Change GTK themes on the flyglade gtk+2 and gtk+3change icon...

How do applicants for an NSF fellowship come up with a research plan for the research statement part of the applications

Is "The life is beautiful" incorrect or just very non-idiomatic?

Why don't electrons take the shorter path in coils?

What is the difference between true neutral and unaligned?

I got kicked out from graduate school in the past. How do I include this on my CV?

Would this system work to purify water?

Does norwegian.no airline overbook flights?

Are there account age or level requirements for obtaining special research?

Compelling story with the world as a villain

How to respectfully refuse to assist co-workers with IT issues?

Why do all fields in a QFT transform like *irreducible* representations of some group?

Shouldn't the "credit score" prevent Americans from going deeper and deeper into personal debt?

Is it safe to remove the bottom chords of a series of garage roof trusses?

Which household object drew this pattern?

Is there a known non-euclidean geometry where two concentric circles of different radii can intersect? (as in the novel "The Universe Between")

LeetCode: Pascal's Triangle C#

Are modern clipless shoes and pedals that much better than toe clips and straps?

Confirming resignation after resignation letter ripped up

Algorithms vs LP or MIP

C++20 constexpr std::copy optimizations for run-time

Earth rotation discrepancy

Is using a hyperlink to close a modal a poor design decision?

Most practical knots for hitching a line to an object while keeping the bitter end as tight as possible, without sag?

Dealing with an extrovert co-worker



GTK+3 change text color in a label (raspberry pi)


Change GTK themes on the flyglade gtk+2 and gtk+3change icon sizes in Gtk+ 3 applicationsHow to uninstall compiled GTK+How can I make text in meld readable?GTK Theme Foreground color not workingGTK applications looking badHow to fix gtk dependencies?Installing GTK+3.22Changing Nemo's Text color using a GTK 3.20+ theme






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







1















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.










share|improve this question

































    1















    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.










    share|improve this question





























      1












      1








      1








      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.










      share|improve this question
















      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.







      gtk3






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 20 at 12:57









      Rui F Ribeiro

      41.3k16 gold badges94 silver badges158 bronze badges




      41.3k16 gold badges94 silver badges158 bronze badges










      asked Jul 21 '18 at 2:03









      user15001user15001

      514 bronze badges




      514 bronze badges

























          3 Answers
          3






          active

          oldest

          votes


















          2















          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.






          share|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.






            share|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 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
            }





            share|improve this answer










            New contributor



            R. Lambert is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.
























              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "106"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f457584%2fgtk3-change-text-color-in-a-label-raspberry-pi%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2















              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.






              share|improve this answer






























                2















                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.






                share|improve this answer




























                  2














                  2










                  2









                  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.






                  share|improve this answer













                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jul 21 '18 at 2:13









                  user15001user15001

                  514 bronze badges




                  514 bronze badges




























                      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.






                      share|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 at 11:05
















                      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.






                      share|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 at 11:05














                      1














                      1










                      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.






                      share|improve this answer















                      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.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jan 17 at 16:55

























                      answered Jan 17 at 16:13









                      Alan CoreyAlan Corey

                      694 bronze badges




                      694 bronze badges
















                      • "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 at 11:05



















                      • "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 at 11:05

















                      "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 at 11:05





                      "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 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
                      }





                      share|improve this answer










                      New contributor



                      R. Lambert is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.


























                        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
                        }





                        share|improve this answer










                        New contributor



                        R. Lambert is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.
























                          0














                          0










                          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
                          }





                          share|improve this answer










                          New contributor



                          R. Lambert is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          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
                          }






                          share|improve this answer










                          New contributor



                          R. Lambert is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.








                          share|improve this answer



                          share|improve this answer








                          edited 2 days ago





















                          New contributor



                          R. Lambert is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.








                          answered 2 days ago









                          R. LambertR. Lambert

                          11 bronze badge




                          11 bronze badge




                          New contributor



                          R. Lambert is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.




                          New contributor




                          R. Lambert is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.



































                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Unix & Linux Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f457584%2fgtk3-change-text-color-in-a-label-raspberry-pi%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

                              Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

                              Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...