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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.