Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: tk canvas text

by tybalt89 (Prior)
on Apr 16, 2018 at 20:13 UTC ( #1213022=note: print w/replies, xml ) Need Help??


in reply to tk canvas text

Just for fun, here's a sort-of-listbox that changes item color and font and is scrollable constructed out of a pane and labels.

#!/usr/bin/perl # http://perlmonks.org/?node_id=1212975 use strict; use warnings; use Tk; use Tk::Pane; my $mw = MainWindow->new; $mw->geometry( '500x400+300+300' ); my @items; my $selectedtext = ''; $mw->Button( -text => 'Exit', -command => sub { $mw->destroy }, )->pack( -side => 'bottom', -expand => 0, -fill => 'x' ); $mw->Label( -textvariable => \$selectedtext, -width => 10, -font => 'courier 30', -fg => 'navy', )->pack( -side => 'right', -fill => 'y' ); my $pane = $mw->Scrolled(Pane => -scrollbars => 'oe', -sticky => 'nsew', )->pack(-fill => 'both', -expand => 1); for my $text ( qw( one two three four five six seven eight nine ten ) +) { my $label = $pane->Label( -text => $text, -fg => 'red', -font => 'courier 40', )->pack(-fill => 'both', -expand => 1); push @items, $label; $label->bind('<ButtonRelease-1>' => sub { $_->configure( -fg => 'red', -font => 'courier 40' ) for @items; $selectedtext = $text; $label->configure( -fg => 'green', -font => 'times 80' ); } ); } MainLoop;

Replies are listed 'Best First'.
Re^2: tk canvas text
by jsteng (Beadle) on Apr 16, 2018 at 23:46 UTC
    Are these Labels within a scrollable frame?
    If so, how come I could not use -justify=>'left' option?
    what options can I use to align the text?
    thanks
      #!/usr/bin/perl # http://perlmonks.org/?node_id=1212975 use strict; use warnings; use Tk; use Tk::Pane; my $mw = MainWindow->new; $mw->geometry( '500x400+300+300' ); my @items; my $selectedtext = ''; $mw->Button( -text => 'Exit', -command => sub { $mw->destroy }, )->pack( -side => 'bottom', -expand => 0, -fill => 'x' ); $mw->Label( -textvariable => \$selectedtext, -width => 10, -font => 'courier 30', -fg => 'navy', )->pack( -side => 'right', -fill => 'y' ); my $pane = $mw->Scrolled(Pane => -scrollbars => 'oe', -sticky => 'nsew', )->pack(-fill => 'both', -expand => 1); for my $text ( qw( one two three four five six seven eight nine ten ) +) { my $label = $pane->Label( -text => $text, -anchor => 'w', -fg => 'red', -font => 'courier 40', )->pack(-fill => 'both', -expand => 1); push @items, $label; $label->bind('<ButtonRelease-1>' => sub { $_->configure( -fg => 'red', -font => 'courier 40' ) for @items; $selectedtext = $text; $label->configure( -fg => 'green', -font => 'times 80' ); } ); } MainLoop;
        @tybalt89,

        Your solution was by far the easiest, simplest and BEST I had in weeks of figuring this little project.
        You saved me from a lot of frustration! :)

        Thank you!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1213022]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2020-10-25 05:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (249 votes). Check out past polls.

    Notices?