Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Disappearing Tk buttons

by NateTut (Deacon)
on Jun 05, 2009 at 15:45 UTC ( #768829=perlquestion: print w/replies, xml ) Need Help??

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

I am a Tk novice, but I've got this little app mostly working with one weird bug. The buttons at the bottom keep disappearing when they are clicked and the window is resized smaller. I have tried packing the various elements each time, but nothing has helped.

Also if anyone has suggestions on a better way to do this I'm all ears. I have included all of the code and the first couple of chapters of the data.

Update:Sorry I can't get the ReadMore Tags to work Nevermind...
#!/usr/bin/perl # -------------------------------------------------------------------- +---------------------------------- # V1.00 # -------------------------------------------------------------------- +---------------------------------- # Display a Specfied or Random Chapter of the Tao Te Ching # Written by Lao-tzu # Translated by S. Mitchell # -------------------------------------------------------------------- +---------------------------------- use strict; use warnings; use constant error => -1; use constant noerror => 0; use constant false => 0; use constant true => 1; use constant MinChapter => 1; use constant MaxChapter => 81; use constant Left => -1; use constant Right => 1; $| = true; # # Modules Used # use Data::Dumper; use File::Basename; use Tk; use Tk::Adjuster; use Tk::Frame; use Tk::Text; # # Who am I? # my $MyName = (fileparse($0, '\..*'))[0]; # # Globals # my $Chapter; my $mw = new MainWindow; my $tf; my $aj; my $bf; my $OutputText; my $LeftButton; my $RightButton; # # GetChapter # # Get the Text of the Specified Chapter # # Arguments: $Chapter # # Returns: @ChapterText # sub GetChapter { my ($Chapter) = shift; my @ChapterText; my $FoundChapterHeading = false; while(<DATA>) { if(/^\Q$Chapter\E$/) { $FoundChapterHeading = true; next; } if($FoundChapterHeading) { if(/^\d+$/) { last; } else { push(@ChapterText, $_); } } } seek(DATA, 0, 0); return(@ChapterText); } # # DisplayChapter # # Display Specified Chapter # # Arguments: $Chapter # # Returns: # sub DisplayChapter { my ($Chapter) = shift; my @ChapterText = GetChapter($Chapter); my $MaxLineLength = 0; foreach(@ChapterText) { if(length($_) > $MaxLineLength) { $MaxLineLength = length($_); } } my $GeometryString = ($MaxLineLength * 8) . 'x' . (scalar(@Chapter +Text) * 20); $mw->geometry($GeometryString); $mw->title(' ' x ((($MaxLineLength - 2) / 2) + 5) . (sprintf('%2d', + $Chapter))); $OutputText->delete('1.0', 'end'); foreach my $Line (@ChapterText) { print(' ' x (($MaxLineLength - length($Line)) / 2) . $Line); } $OutputText->pack(qw/-side bottom -fill both -expand 1/); } # # ButtonHandler # # Process Button Presses # # Arguments: $ButtonType # # Returns: # sub ButtonHandler { my ($Type) = shift; $Chapter += $Type; if($Chapter < MinChapter) { $Chapter = MaxChapter; } elsif($Chapter > MaxChapter) { $Chapter = MinChapter; } DisplayChapter($Chapter); } # # Main # if($ARGV[0] and $ARGV[0] > 0 and $ARGV[0] < 82) { $Chapter = $ARGV[0]; } else { $Chapter = int(rand(81)) + 1; } $tf = $mw->Frame; # Top Frame; $aj = $mw->Adjuster(-widget => $tf, -side => 'bottom', -restore => tru +e); $bf = $mw->Frame; # Bottom Frame $OutputText = $tf->Text ( -height => '1' , -width => '1' , ); $LeftButton = $bf->Button ( -command => [ \&ButtonHandler, Left ] , -height => '1' , -text => '<' , -width => '1' , )->pack ( -side => 'left' , -fill => 'both' , -expand => true ); $RightButton = $bf->Button ( -command => [ \&ButtonHandler, Right ] , -height => '1' , -text => '>' , -width => '1' , )->pack ( -side => 'left' , -fill => 'both' , -expand => true ); tie *STDOUT, ref $OutputText, $OutputText; $tf->pack(qw/-side top -fill both -expand 1/); $aj->pack(qw/-side top -fill y/); $bf->pack(qw/-side top -fill y/); $LeftButton->pack; $RightButton->pack; DisplayChapter($Chapter); MainLoop; __DATA__ 1 The tao that can be told is not the eternal Tao The name that can be named is not the eternal Name. The unnamable is the eternally real. Naming is the origin of all particular things. Free from desire, you realize the mystery. Caught in desire, you see only the manifestations. Yet mystery and manifestations arise from the same source. This source is called darkness. Darkness within darkness. The gateway to all understanding. 2 When people see some things as beautiful, other things become ugly. When people see some things as good, other things become bad. Being and non-being create each other. Difficult and easy support each other. Long and short define each other. High and low depend on each other. Before and after follow each other. Therefore the Master acts without doing anything and teaches without saying anything. Things arise and she lets them come; things disappear and she lets them go. She has but doesn't possess, acts but doesn't expect. When her work is done, she forgets it. That is why it lasts forever. 3 . . .

Replies are listed 'Best First'.
Re: Disappearing Tk buttons
by cmv (Chaplain) on Jun 05, 2009 at 18:41 UTC

    When I run your script, I don't see the buttons disappear. Instead I see the mainwindow resizing to be very small. If I manually expand the window, the buttons are still there. This is probably due to the fact that you generate a random chapter number if one isn't given (as the first argument). In the example script we only have 3 chapters of data, and I was given the random number of 58 (you may want to put in error checking so that the random number generated falls within the number of chapters you have in your data, instead of a hardcoded number).

    Ok, that was something unique to your example script. If I force the script to start at chapter 1 (by giving it the number one as an argument 1 , it seems to work correctly for me on the first and second chapters. The third chapter is where things get goofed up.

    The first thing I noticed was how you put the text into the text window (tie *STDOUT, ref $OutputText, $OutputText;). I thought maybe that had something to do with the problem, so I changed your script to simply use $OutputText->insert('end', "formatted text");. That didn't fix the problem, but I'd recommend doing it this way (unless there's a good reason not to) - It's just easier to understand.

    Then, I commented out where you set the mainwindow geometry and ran it again. Now it works fine, except the mainwindow stays the same size all the time.

    It seems that when you set the mainwindow geometry below a certain threshold, it won't reset to something bigger anymore. I would suggest a minimum width and height for your main window.

    Hope that helps.


Re: Disappearing Tk buttons
by shmem (Chancellor) on Jun 05, 2009 at 19:09 UTC

    What cmv said, and: drop $mw->Adjuster(); it gets confused when you re-pack your $OutputText in sub DisplayChapter.

Re: Disappearing Tk buttons
by lamprecht (Friar) on Jun 05, 2009 at 22:01 UTC
    this was not your question, but instead of inserting spaces you can get centered lines by tagging them like so:
    use Tk; use strict; use warnings; my $mw = tkinit; my $text = ''; $text .= 'x' x rand(40) ."\n" for (1..20); my $t = $mw->Text()->pack; $t->tagConfigure(centered => -justify => 'center'); $t->insert('end',$text,'centered'); MainLoop();
    Cheers Christoph
      Thanks I was never happy with the way I was doing that.
Re: Disappearing Tk buttons
by NateTut (Deacon) on Jun 05, 2009 at 19:49 UTC
    Thanks for the help. I did away with the tie to STDOUT, that was from a tutorial I started with. I increased the vertical multiplier which seemed to solve the problem, but often leaves too much white space at the bottom. I guess I was looking for an easy way to size the window nicely around the text.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://768829]
Approved by ikegami
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2019-10-16 04:26 GMT
Find Nodes?
    Voting Booth?