#!/usr/bin/perl ################################################## # tkpauk -- Mike Schilli, 2001 (m@perlmeister.com) ################################################## use warnings; use strict; use Tk; use Tk::FileSelect; my $width = 400; # Fensterbreite my $lheight = 10; # Texthöhe my $lwidth = 50; # Textbreite my @SHUFFLED; my %TRANS; my $TOP = MainWindow->new(-width => $width); my $menubar = $TOP->Menu(-type => 'menubar'); my $f = $menubar->cascade(-label => 'File'); $f->command(-label => 'Open', -command => \&file_select); $f->command(-label => 'Exit', -command => sub { exit(0) }); $TOP->configure(-menu => $menubar); my $QUESTION = $TOP->Label( -width => $lwidth, -height => $lheight, -wraplength => 7/10*$width, )->pack(); my $ANSWER = $TOP->Label( -width => $lwidth, -height => $lheight, -wraplength => 7/10*$width, )->pack(); my $FOOTLINE = $TOP->Frame(); my $COUNTER = $FOOTLINE->Label(); my $MESSAGE = $FOOTLINE->Label(); $MESSAGE->pack(-expand => 'yes', -side => 'left'); $COUNTER->pack(-expand => 'no', -anchor => 'e'); $FOOTLINE->pack(-expand => 'yes', -fill => 'x'); state_init(); MainLoop(); ################################################## sub footline { ################################################## my($text) = @_; my $nof_items = @SHUFFLED; $COUNTER->configure(-text => $nof_items); $MESSAGE->configure(-foreground => "darkgrey"); $MESSAGE->configure(-text => "$text"); } ################################################## sub file_select { ################################################## my $f = $TOP->FileSelect(-directory => ".", -filter => '*.txt'); my $file = $f->Show(); state_ask(), return unless defined $file; %TRANS = (); open DAT, "<$file" or die "Cannot open $file"; while(my $line = ) { my($q, $a) = split /\s*--\s*/, $line, 2; $TRANS{$q} = $a; } close DAT; @SHUFFLED = shuffle(keys %TRANS); state_ask(); } ################################################## sub shuffle { ################################################## my @array = @_; for(my $i=@array; --$i; ) { my $j = int rand ($i+1); next if $i == $j; @array[$i,$j] = @array[$j,$i]; } return @array; } ################################################## sub state_init { ################################################## footline("Please select a data file"); $QUESTION->configure(-text => ""); $ANSWER->configure(-text => ""); map { $TOP->bind("<$_>" => "") } qw(Return space Button-1 Button-3); file_select(); } ################################################## sub state_ask { ################################################## return unless @SHUFFLED; footline("Enter: See translation"); $QUESTION->configure(-text => $SHUFFLED[0]); $ANSWER->configure(-text => ""); $TOP->bind("" => \&state_init); map { $TOP->bind("<$_>" => \&state_show) } qw(Return space Button-1 Button-3); } ################################################## sub state_show { ################################################## footline( "Enter: Item processed Space: Keep item"); $QUESTION->configure(-text => $SHUFFLED[0]); $ANSWER->configure(-text => $TRANS{$SHUFFLED[0]}); $TOP->bind("" => \&success); $TOP->bind("" => \&success); $TOP->bind("" => \&failure); $TOP->bind("" => \&failure); } ################################################## sub state_congrats { ################################################## footline(""); $QUESTION->configure(-text => "Congratulations!!!"); $ANSWER->configure(-text => ""); map { $TOP->bind("<$_>" => \&state_init) } qw(Return space Button-1 Button-3); } ################################################## sub success { ################################################## shift @SHUFFLED; @SHUFFLED ? state_ask() : state_congrats(); } ################################################## sub failure { ################################################## my $current = shift @SHUFFLED; push @SHUFFLED, $current; state_ask(); }