Here's a little ASCII Table I whipped up when I needed to look at ASCII Values in Tk. Its not fancy but it does the trick.
use strict; use warnings; use diagnostics; use Tk; my $Columns_per_Row = 12; sub ASCII_Buttons { my $root = MainWindow->new(); $root->title('Tk ASCII Table'); my ($row,$col) = (1,1); for my $character (0..255) { my $label = sprintf("[%03d][%s]", $character, chr($character)) +; $root->Button ( -text => $label, -font => '-*-Lucida Console-Bold-R-Normal-*-*-160-*- +*-*-*-*-*', )->grid ( -row => $row, -column => $col, -sticky => 'nswe' ); if ($col >= $Columns_per_Row) { $row++; $col = 0; } $col++; } } ASCII_Buttons(); MainLoop;
Update:
Changed code to reflect mawe's suggestions. Thanks!

Replies are listed 'Best First'.
Re: Tk ASCII Table
by mawe (Hermit) on Jun 08, 2005 at 06:02 UTC
    Hi!

    Your code can be shortened a bit:

    use strict; use warnings; use Tk; my $columns_per_row = 12; sub ASCII_Buttons { my $root = MainWindow->new(); $root->title('MMG Server Options'); my ($row,$col) = (1,1); for my $character (0..255) { my $label = sprintf("[%03d][%s]", $character, chr($character)) +; $root->Button( -text=>$label, -font => '-*-Lucida Console-Bold-R-Normal-*-*-160-*-*- +*-*-*-*', )->grid(-row=>$row,-column=>$col,-sticky=>'nswe'); if ($col>=$columns_per_row) {$row++;$col=0} $col++; } } ASCII_Buttons(); MainLoop;

    Regards, mawe

      Thanks, I've always wondered if the grid stuff had to come after. I picked up the two step method from my use of SpecTcl to generate the basic UI.
Re: Tk ASCII Table
by ww (Archbishop) on Jun 08, 2005 at 14:19 UTC
    nicely done, NatTut.

    suggestion of less consequence (and perhaps not a needed exercise for you): create scrollbars for those user cases where right and/or bottom runs off screen.

      That would be nice, but I'm having trouble getting scrollbars to work. I can't seem to get the $root window to have scrollbars so I thought of putting everything in a frame and scrolling that, but that didn't work either.
        Hi!

        Here's a version with scrollbars:

        use strict; use warnings; use Tk; my $columns_per_row = 12; sub ASCII_Buttons { my $root = MainWindow->new(); $root->title('MMG Server Options'); my $frame = $root->Scrolled('Frame', -width=>600,-height=>400)->pack(-fill=>'both', -expand=>1); my ($row,$col) = (1,1); for my $character (0..255) { my $label = sprintf("[%03d][%s]", $character, chr($character)) +; $frame->Button( -text=>$label, -font => '-*-Lucida Console-Bold-R-Normal-*-*-160-*-*- +*-*-*-*', )->grid(-row=>$row,-column=>$col,-sticky=>'nswe'); if ($col>=$columns_per_row) {$row++;$col=0} $col++; } } ASCII_Buttons(); MainLoop;

        Regards, mawe