Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Centring text in a WxPerl static text box

by HenryLaw (Initiate)
on Feb 13, 2018 at 23:30 UTC ( #1209097=perlquestion: print w/replies, xml ) Need Help??
HenryLaw has asked for the wisdom of the Perl Monks concerning the following question:

The following code, based on an example in http://learnperl.scratchcomputing.com/tutorials/wxperl/, is a minimum example which is supposed to put up a StaticText control with its legend centred. Instead it is resolutely left-aligned. I have tried everything I can think of, including putting wxALIGN_CENTRE on the sizer add, calling the CentreOnParent method of the text box ... and a dozen other things I can't remember.

What am I doing wrong?

#!/usr/bin/env perl use warnings; use strict; use Wx; use wxPerl::Constructors; package MyApp; use base 'Wx::App'; use Wx qw( wxALIGN_CENTRE wxVERTICAL wxEXPAND ); sub OnInit { my $self = shift; my $frame = wxPerl::Frame->new(undef, 'A wxPerl Application'); $frame->SetMinSize([120,40]); my $sizer = Wx::BoxSizer->new(wxVERTICAL); my $text = wxPerl::StaticText->new( $frame, "This is text", style => wxALIGN_CENTRE ); $sizer->Add( $text, 1, wxEXPAND ); $frame->SetSizer($sizer); $frame->Show; } MyApp->new->MainLoop;

Replies are listed 'Best First'.
Re: Centring text in a WxPerl static text box
by beech (Vicar) on Feb 14, 2018 at 05:36 UTC

    The following code, based on an example in http://learnperl.scratchcomputing.com/tutorials/wxperl/, is a minimum example which is supposed to put up a StaticText control with its legend centred. Instead it is resolutely left-aligned. I have tried everything I can think of, including putting wxALIGN_CENTRE on the sizer add, calling the CentreOnParent method of the text box ... and a dozen other things I can't remember. What am I doing wrong?

    Hi,

    What version are you using?

    wxALIGN_CENTRE seems to do the trick for me, without it the text is left aligned

    Perl 5.016001 Wx 0.9922 wxWidgets 2.9.4

     print join( ' ', "\n", Perl => $], Wx => $Wx::VERSION, Wx::wxVERSION_STRING, "\n" );

    Also, I wouldn't expect a wxVERTICAL box sizer to try to align horizontally :)

      > What version are you using?

      Perl 5.022001 Wx 0.9923 wxWidgets 3.0.2

      > wxALIGN_CENTRE seems to do the trick for me, without it the text is left aligned

      When you say "Does the trick", do you mean my code gives you centred text? Aaagh!

      > Also, I wouldn't expect a wxVERTICAL box sizer to try to align horizontally :)

      Quite so; but I did try the wxALIGN_CENTRE flag on the sizer's Add method, without any obvious difference. And in any case the plan was that the wxALIGN_CENTRE flag on the StaticText box itself was going to do the alignment.

      Thinking about what you said, I wonder if the StaticText box is actually only the size of the text, so that centring the text within it has no effect.

      Spent most of the afternoon fiddling with this. I'm convinced that the wxALIGN_CENTRE flag doesn't work.

      I've written a little subroutine which does what I want; it's OK but it shouldn't be necessary

      sub centre_text{ # centre_text( $text_control, $container ); # Not enough error checking, really ... my ( $text_control, $container ) = @_; my ($container_width) = $container->GetSizeWH(); my ($text_width) = $container->GetTextExtent( $text_control->GetLabe +l() ); my ( $text_x, $text_y ) = $text_control->GetPositionXY(); $text_control->Move( ($container_width-$text_width)/2, $text_y ); }
Re: Centring text in a WxPerl static text box
by NetWallah (Canon) on Feb 14, 2018 at 03:00 UTC
    Try the American spelling : wxALIGN_CENTER.

    The perl Wx::DemoModules::wxStaticText module has this declaration:

    sub styles { my( $self ) = @_; return ( [ wxALIGN_LEFT, 'Align left' ], [ wxALIGN_CENTER, 'Align center' ], [ wxALIGN_RIGHT, 'Align right' ], [ wxST_NO_AUTORESIZE, 'No autoresize' ], ); }

                    Python is a racist language what with it's dependence on white space!

      Almost uniquely in my experience, Wx, and therefore WxPerl, accepts both spellings; see http://docs.wxwidgets.org/2.8.12/wx_wxstatictext.html#wxstatictext

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1209097]
Approved by beech
Front-paged by 1nickt
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2018-05-24 19:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?