Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

tk canvas text

by jsteng (Beadle)
on Apr 16, 2018 at 10:12 UTC ( [id://1212975]=perlquestion: print w/replies, xml ) Need Help??

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

Hi
I am trying to make a customized TK::Listbox using TK::canvas because I need the ability to change text color and font when <double-1>
here is my initial work:
#!/usr/bin/perl use warnings; use strict; use Tk; my ($DimX,$DimY)=(400,600); my ($mX,$mY) = (2,2); my $mw = MainWindow->new( -bg=> "#000000", -borderwidth=> 0); $mw->minsize( $DimX, $DimY ); $mw->maxsize( $DimX, $DimY ); my %canvas; my $fontsize=int(20); $canvas{'canvas'} = $mw->Scrolled( 'Canvas', -width => $DimX-2*$mX, -height => 2*$DimY, -bg => '#60C8BC', -borderwidth => 2, -relief => 'raised', -scrollbars => 'e', )->pack(); $canvas{'canvas'}->CanvasBind("<Button-1>", [ \&print_xy, Ev('x'), Ev( +'y') ]); sub print_xy { my ($canv, $x, $y) = @_; my $sel = int($canvas{'canvas'}->canvasy($y) / (1.4*$fontsize+2)); printf "Selected: %d, (%d,%d)\n", $sel, $canvas{'canvas'}->canvasx +($x),$canvas{'canvas'}->canvasy($y); $canvas{'text'}{$sel}->itemconfigure( -fill => '#0000FF', -font => "Courier $fontsize bold", ); } for (my $y=0; $y<200; $y++) { $canvas{'text'}{$y} = $canvas{'canvas'}->createText( 5, 4+$y*(1.4* +$fontsize+2), -fill => '#000000', -text => "GgYy".$y x 20, -font => "Courier $fontsize normal", -anchor => 'nw', ); } MainLoop;
Problems:
1. I could not make the scrollbar scroll.
2. How do I change the selected item's font/color?
I tried doing this:
$canvas{'text'}{$sel}->itemconfigure( -fill => '#0000FF', -font => "Courier $fontsize bold", );
But it is not working.
3. lastly, how do I retrieve a particular selected text's font/color? Sorry folks, I am very rusty with perl after years of not programming.

Thanks BTW: My original (>10 years ago) script used TK::ROText; however in that version, the windows' size is fixed, and all entry in the ROText are space padded to cover the whole line (sprintf "%-50s", $text). Then using tag for each line. That was a nightmare, it was ugly programming. I am redoing that script to use Listbox; it was alot cleaner but I could not change font of selected text (line) thus I am redoing it with Canvas.

Replies are listed 'Best First'.
Re: tk canvas text
by tybalt89 (Monsignor) on Apr 16, 2018 at 20:13 UTC

    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;
      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;
Re: tk canvas text
by Anonymous Monk on Apr 16, 2018 at 10:17 UTC
    Tags
      what about tag? a sample script would be helpful. thanks!
        Read about it in tk docs

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1212975]
Approved by Discipulus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-04-20 10:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found