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

Hello Perl Monks

I have a question about how to provide a way for the user to control the height (in number of lines) of a text box

I am running strawberry perl 5.24 under windows 7.

My main window has an expandable list box on top and a fixed height text box on bottom. So if the user uses the mouse to grow the main window, the height of the list box on top increases, but the height of the text box on the bottom stays the same. See the code below for the details.

What I would like to do is make controls to allow the user to adjust the height of the text box, preferably without affecting the contents of either the list box or the text box. So if the user expands the main window, the height of the text box will still remain fixed at the value set by the user.

In the code below, I tried to use the configure method to change the height, but it had no effect. Any suggestions or advice would be appreciated.

use strict; use warnings; use Cwd; use Tk qw( MainLoop ); require Tk::ROText; { my $startb; my $textbox; my $mw = MainWindow->new(-background => 'gray50' ); my $listbox = $mw->Scrolled('Listbox', -font => "{Courier New} 12 bold", -background => 'DarkCyan', -foreground => 'OldLace', -scrollbars => 'se', -selectmode => "single", )->pack(-expand => 1, -fill => 'both'); $textbox = $mw->Scrolled('ROText', -font => "{Courier New} 12 bold", -background => 'DarkBlue', -foreground => 'OldLace', -scrollbars => 'se', -wrap => 'none', -height => 8 )->pack(-expand => 0, -fill => 'x', -anchor => 'n'); my $buttonFrame = $mw->Frame()->pack(-side => 'bottom', -fill=>'both +', -expand=>0); # Button to increase the height of the text box my $growb = $buttonFrame->Button( -text => '+', -command => sub { $textbox->packForget; $textbox->configure(-height => 11); $textbox->pack(-expand => 0, -fill => 'x +', -anchor => 'n'); $textbox->configure(-background => 'red1'); $mw->idletasks; }, )->pack(-side => "left", -fill => "b +oth", -expand => 0); # Button to decrease height of text box my $shrinkb = $buttonFrame->Button( -text => '-', -command => sub { $textbox->packForget; $textbox->configure(-height => 6); $textbox->pack(-expand => 0, -fill => 'x', + -anchor => 'n'); $textbox->configure(-background => 'DarkBl +ue'); $mw->idletasks; }, )->pack(-side => "left", -fill = +> "both", -expand => 0); MainLoop(); }

Replies are listed 'Best First'.
Re: Question about how to implement user control over text box height
by shmem (Chancellor) on Jul 06, 2017 at 15:34 UTC

    You might want to use Tk::Adjuster...

    ... $textbox = $mw->Scrolled( 'ROText', -font => "{Courier New} 12 bold", -background => 'DarkBlue', -foreground => 'OldLace', -scrollbars => 'se', -wrap => 'none', -height => 8 ); my $adj = $mw->Adjuster( -widget => $textbox, -side => 'bottom'); $adj->pack( -side => 'top', -fill => 'x'); $textbox->pack( -expand => 0, -fill => 'x', -anchor => 'n' ); my $buttonFrame = $mw->Frame()->pack( -side => 'bottom', -fill => 'both', -expand +=> 0 ); ...

    ...which allows you to adjust the textbox size with the mouse.

    The Scrolled widget is a compound widget, and the ROText widget can be retreived via $textbox->Subwidget('scrolled'). Changing its size and repacking it just changes the size of the text pane, not the entire Scrolled widget (scrollbars, corners and outer frame). I've tried various ways of packForget and pack on them, to no avail.

    But the Adjuster just does the job ;-)

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re: Question about how to implement user control over text box height
by zentara (Cardinal) on Jul 06, 2017 at 14:37 UTC
    Hi, I think the problem is that you are using a Scrolled ROText widget, and therefore your $textbox is actually a scrolled widget, and will not respond to the -height option. I think you should test this with a plain ROText widget, and put the ROText widget into a scrolled pane to get the scrolling. Busy now, but I will try to see if I can get that idea to work. You can also try using the Subwidget method to get the real text widget.
    my $realtextbox = $textbox->Subwidget('Scrolled');
    and try to manipulate the $realtextbox's -height option.

    If worse comes to worse, you could completely packForget the $mw, and rebuild it, but that would cause an annoying screen flicker, and necessitate saving any text data, and reinserting it after the rebuild.


    I'm not really a human, but I play one on earth. ..... an animated JAPH
Re: Question about how to implement user control over text box height
by zentara (Cardinal) on Jul 06, 2017 at 19:29 UTC
    UPDATE: oops, I realize solution this dosn't actually increase the number of viewable text lines. It needs a bit more work. :-(

    Hi, this seems to do what you want. Notice the $pane is packed after the ROText is added to it. Just for your info, the Scrolled Pane is an excellent widget, as it handles the scrollbars very well.

    #!/usr/bin/perl use strict; use warnings; use Cwd; use Tk qw( MainLoop ); require Tk::ROText; { my $startb; my $textbox; my $mw = MainWindow->new(-background => 'gray50' ); my $listbox = $mw->Scrolled('Listbox', -font => "{Courier New} 12 bold", -background => 'DarkCyan', -foreground => 'OldLace', -scrollbars => 'se', -selectmode => "single", )->pack(-expand => 0, -fill => 'x'); my $pane=$mw->Scrolled('Pane',-scrollbars => 'se'); $textbox = $pane->ROText(-font => "{Courier New} 12 bold", -background => 'DarkBlue', -foreground => 'OldLace', -wrap => 'none', -height =>8 )->pack(-expand => 0, -fill => 'x', -anchor => 'n'); $pane->pack(-expand => 1, -fill => 'x'); my $buttonFrame = $mw->Frame()->pack(-side => 'bottom', -fill=>'both', + -expand=>0); # Button to increase the height of the text box my $growb = $buttonFrame->Button( -text => '+', -command => sub { $textbox->packForget; $textbox->configure(-height => 11); $textbox->pack(-expand => 0, -fill => 'x', -anchor => 'n'); $textbox->configure(-background => 'red1'); $mw->idletasks; $mw->packPropagate(0); }, )->pack(-side => "left", -fill => "both", -expand => 0); # Button to decrease height of text box my $shrinkb = $buttonFrame->Button( -text => '-', -command => sub { $textbox->packForget; $textbox->configure(-height => 6); $textbox->pack(-expand => 0, -fill => 'x', -anchor => 'n'); $textbox->configure(-background => 'DarkBlue'); $mw->idletasks; }, )->pack(-side => "left", -fill => "both", -expand => 0); MainLoop(); }

    I'm not really a human, but I play one on earth. ..... an animated JAPH
Re: Question about how to implement user control over text box height
by CrashBlossom (Beadle) on Jul 06, 2017 at 21:30 UTC

    Thanks shmem, for the tip on Adjuster. I had never heard of this widget, but it appears to provide exactly the functionality I was looking for.

Re: Question about how to implement user control over text box height
by CrashBlossom (Beadle) on Jul 06, 2017 at 21:34 UTC

    Zentara, Thanks for your replies. I'm new to Tk, so your comments have given me some new ideas to explore.