stevieb has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on a GUI for one of my projects (I dislike UI programming, specifically non-web), which will be as basic and simple as possible. I chose Wx, simply because it's a tad bit prettier (than Tk). This UI is being designed for a 5" touch-screen, and doesn't need many features.

I'm wondering with Wx, how I can change the font colour of a simple StaticText (or even a button). The docs are sparse, but even the perl and XS code don't seem to have anything relating to changing colours.

Am I missing something, or is this just the way Wx is? If Tk or (insert suggestion here) can be manipulated to do simple things like change the colour of a font, I'll seriously consider changing to that (to begin with, Wx was a PITA to get set up on a Raspberry Pi, so I'm half-way toward changing as-is).

my $font = Wx::Font->new(16); $self->{t_header} = Wx::StaticText->new( $panel, 1, "Temperature", $self->{xy}{t_header} ); $self->{t_header}->SetFont($font);

update: The code and references to other code in this and subsequent nodes require trunk installs of certain modules. CPAN versions will not have the updates to render things properly...

Replies are listed 'Best First'.
Re: Changing font colour in a Wx StaticText or Button
by james28909 (Deacon) on Aug 29, 2016 at 00:07 UTC
    here is what i do:
    my $tc = Wx::TextCtrl->new( $window1, -1, '', [450,15], [229,300], wxT +E_READONLY|wxTE_RICH|wxALL); my ( $red, $green ) = ( Wx::Colour->newRGB( 255, 0, 0 ), Wx::Colour->n +ewRGB( 0, 90, 0 ) ); if ( $md5s =~ $md5 ) { my $style = Wx::TextAttr->new(); $style->Wx::TextAttr::SetTextColour($green); $tc->SetStyle( -1, -1, $style ); $tc->WriteText("$file | $filesize\n"); } else { my $style = Wx::TextAttr->new(); $style->Wx::TextAttr::SetTextColour($red); $tc->SetStyle( -1, -1, $style ); $tc->WriteText("$file | $filesize\n"); }

    I dont know if that will help much, but maybe it will help enough!

    EDIT: I think this is might be for TextCtrl. I am sure you can do it with static text though. I look through my code.

    EDIT2: Here I found something:

    my $static_text = Wx::StaticText->new( $window, -1, "Current File:", [ + 5, 355 ]); $static_text->SetForegroundColour( Wx::Colour->new(255, 0, 0) );
    And it does indeed work. This turned the static text "Current File:" red. Deleting the SetForegroundColour line returns it to black.
Re: Changing font colour in a Wx StaticText or Button
by beech (Parson) on Aug 28, 2016 at 21:56 UTC

    Hi,

    Isn't a wxStaticText another wxWindow ? So SetForegroundColour or something?

    :)

      Thanks beech, the level of subclassing is pretty deep, so I'll try to figure out where to apply this in my currently spaghetti code and see what's up.

      Forgive the code. This is my first attempt ever at writing a GUI in Perl, so I'm just learning the very basic ropes :)

      Guidance be welcomed.

      I quiver when I think that my more elaborate distributions aren't clear in their documentation... (that said, I realize that this is a *massive* undertaking, wrapping over all of Wx).

      I can't find any documentation or examples of "how-to" extending a class or doing what I want, so I think I'll contact the author to see. I didn't want to spend a whole day writing a little gui app.

      Is Wx the way to go in Perl for GUI? Tk looks like it has much better documentation. Despite that it doesn't look as nice (realistically, that won't matter to me), the fact that it is far more prevalent and apparently wider-used, I might start over with it.

      My goals for the GUI are ensure functionality with the back-end actions, ease of writing, ease of changes, ease of "I need to do this..." type searches.

        um ... I like sweaters

        #!/usr/bin/perl -- use strict; use warnings; use Wx qw/ :allclasses /; my $frame = Wx::Frame->new( undef, -1, "statictext color change" ); $frame->{panel} = Wx::Panel->new( $frame ); $frame->{button} = Wx::Button->new( $frame->{panel} , -1, "Toggle", ) +; $frame->{static} = Wx::StaticText->new( $frame->{panel}, -1, "I am co +lorful" ); $frame->{sizer} = Wx::BoxSizer->new( Wx::wxVERTICAL() ); $frame->{panel}->SetSizer( $frame->{sizer} ); $frame->{sizer}->Add( $frame->{button} ); $frame->{sizer}->AddSpacer(100); $frame->{sizer}->Add( $frame->{static} ); Wx::Event::EVT_BUTTON( $frame, $frame->{button}, \&button_click, ); $frame->Show; Wx::SimpleApp->new->MainLoop; exit( 0 ); sub button_click { my( $frame, $event ) = @_; $frame->{hit}++; my $static = $frame->{static}; my $color = $static->GetForegroundColour; my $label = $static->GetLabel; $label =~ s/ ## .*$//; $label .= " ## ".localtime; $static->SetLabel( $label ); if( $frame->{hit} % 2 ){ $static->SetForegroundColour( Wx::wxBLUE() ); } else { $static->SetForegroundColour( Wx::wxRED() ); } $static->Update; }
        I have a program I written in wx and it works freaking great. I change color of the text depending on its md5 value in list box and statictext i think. let me see if I can find the script and see if I have something that can help.
Re: Changing font colour in a Wx StaticText or Button
by stevieb (Canon) on Aug 30, 2016 at 13:44 UTC

    Thanks for all the help and responses everyone.

    After finding Wx::Demo and glancing over much of that, Wx is quite a bit easier to use once you take a couple of hours to understand the hierarchy of its classes. Not only that, it led to realizing better and more concise ways to get everything set up in the first place.

    beech was right. It was as simple as:

    sub aux4_on { my $self = shift; $self->{aux4_pin}->write(HIGH); $self->{aux4_display}->SetLabel("on"); $self->{aux4_display}->SetForegroundcolour( Wx::Colour->new(0, 204, 0) # green ); }

      To boot, I also came across a kickass wiring schematic CAD application which is open source: Fritzing.

        I found Fritzing about two years ago. I use it to draw stripboard layouts. Haven't tried to have a PCB made yet.

        James

        There's never enough time to do it right, but always enough time to do it over...

Re: Changing font colour in a Wx StaticText or Button
by jmlynesjr (Deacon) on Aug 29, 2016 at 18:29 UTC

    For more examples see github.com/jmlynesjr.

    Also go to wxwidgets.org and download the html formatted documentation, then go through the learning curve to figure out how to translate it to wxPerl.

    James

    There's never enough time to do it right, but always enough time to do it over...

Re: Changing font colour in a Wx StaticText or Button
by james28909 (Deacon) on Aug 30, 2016 at 11:19 UTC

    I should also add that I am using Active State Perl version 5.16.3 and have no reason to update. Im using Alien-Wxwidgets 0.65 from Mark Dootson, Wx version 0.9921 by Mattia Barbon, and I also installed the Wx demo along time ago (which did help tremendously). i can pack these up and email them to you if you have trouble finding them. I was having a ton of problems until I installed Wx from Mattia Barbon.