Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

how to write a vertical TK Entry?

by jsteng (Beadle)
on Mar 05, 2020 at 04:16 UTC ( [id://11113817]=perlquestion: print w/replies, xml ) Need Help??

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

I have:
$mw->Entry(-text => "1 2 3 4 5", relief => 'raised', -borderwidth => 1, )->place( -y=>0, -x=>0,  -width=>50, -height=>200);


How do I make it display vertically, either rotated 90*, or next character displayed next line like:
1
2
3
4
5
(is there a word-wrap option?) thanks!

Replies are listed 'Best First'.
Re: how to write a vertical TK Entry?
by kcott (Archbishop) on Mar 05, 2020 at 08:43 UTC

    G'day jsteng,

    To the best of my knowledge, you cannot do this with Tk::Entry; however, you can code this functionality with Tk::Text.

    The following code is fully functional. It shows how to set up the pseudo-entry widget (note the initial text has no spaces; just 12345). The "Latest Entry" button will retrieve the full, possibly edited, text (note the chomp) and update the display above.

    #!/usr/bin/env perl use strict; use warnings; use Tk; my $mw = MainWindow::->new(-title => 'Vertical Entry'); $mw->geometry('300x400+40+40'); my $string = '12345'; my $entry = $mw->Text(-width => 1, -wrap => 'char'); $entry->pack(-fill => 'y', -side => 'left'); $entry->insert('1.0', $string); $mw->Label(-textvariable => \$string) ->pack(-fill => 'x', -side => 'top'); $mw->Button(-text => 'Latest Entry', -command => sub { chomp($string = $entry->get('1.0', 'end')) }) ->pack(-fill => 'x', -side => 'top'); $mw->Button(-text => 'Exit', -command => sub { exit }) ->pack(-side => 'bottom'); MainLoop;

    It looks pretty weird to me, but that's what you asked for. :-)

    — Ken

Re: how to write a vertical TK Entry?
by Marshall (Canon) on Mar 05, 2020 at 06:35 UTC
    I think you will need to make separate Entry objects. To get you started:
    use strict; use warnings; use Tk; my $mw = MainWindow->new(); foreach (qw (1 2 3 4 5) ) { gen_entry_object ($mw, "Entry Window $_"); } MainLoop; sub gen_entry_object { my $mw = shift; my $entry = shift; $mw->Entry(-text => "$entry" )->pack; }
    In general, the pack() geometry manager is the easiest to use. The key is to make "invisible frames" that you pack other objects into. You can pack these "invisible frames" to the left, right, up, down of each other. It has been years since I did any significant GUI work, but hand drawing a sketch of what you want before writing the code is extremely helpful. Now an entry object will have a reference to a memory location of the entered data. I didn't show that and you didn't either. A lot of experimentation is required for writing a fancy GUI, but it certainly can be done!
Re: how to write a vertical TK Entry?
by AnomalousMonk (Archbishop) on Mar 05, 2020 at 07:55 UTC

    Don't know how to rotate the characters, but...

    use strict; use warnings; use Tk; use Tk::Text; my $MW = MainWindow->new; my $text_frame = $MW->Frame->pack; my $v_text = $text_frame->Text( qw(-relief flat -width 1 -height 10 -wrap char) )->pack(qw(-side left)); my $xy_text = $text_frame->Text( qw(-relief flat -width 20 -height 10 -wrap word) )->pack(qw(-side left)); $v_text->insert('1.0', '12345'); $xy_text ->insert('1.0', <<"EOT"); now is the time the rain in spain how now brown cow EOT $v_text->insert('end', 'abc'); $xy_text ->insert('end', <<"EOT"); four score and seven years ago... EOT MainLoop;


    Give a man a fish:  <%-{-{-{-<

Re: how to write a vertical TK Entry?
by Marshall (Canon) on Mar 05, 2020 at 11:54 UTC
    Ok, I took another stab at this... It's been a very, very long time since I did Tk.

    This code provides 5 vertical entry fields which can be edited (that's what you can do to an entry field as opposed to a label or a button). You press the 'Print Values' button to see the edited values of these entry windows. I omitted the pack() details which can specify width, etc and also wrap around if desired. Tweaking these details can be time consuming.

    The main point here is whether or not I have the general idea of what you wanted?

    use strict; use warnings; use Tk; use Data::Dumper; my $mw = MainWindow->new(); my @entry_refs; foreach (qw (1 2 3 4 5) ) { push @entry_refs, create_entry_object ($mw, "Default Value is: $_" +, ); } $mw->Button(-text => 'Print Values', -command => \&list_entries)->pack +(); MainLoop; sub list_entries { print "\n"; foreach my $ref (@entry_refs) { print "$$ref\n"; } } sub create_entry_object { my $window = shift; my $text_var = shift; $window->Entry(-textvar => \$text_var )->pack; return \$text_var; }
    Update: I looked back at this code and it appears to me that you need some Label objects to the left of the "ENTRY" objects. Take a stab at that and let us know how you are doing.
Re: how to write a vertical TK Entry?
by tybalt89 (Monsignor) on Mar 08, 2020 at 18:13 UTC

    Out of half curiosity, half boredom, and half fun, I tried to build a (very incomplete) vertical entry from a Label widget using newlines to get a vertical display.

    #!/usr/bin/perl use strict; use warnings; use Tk; my $text = 'Vertical'; my $entry = 'Entry'; my $mw = MainWindow->new; $mw->geometry( '+600+200' ); $mw->Label(-text => "Two\nVerticalEntry\nWidgets", -fg => 'blue', -font => 'courierbold 20',)->pack; my $middle = $mw->Frame->pack; verticalentry( $middle, \$text, 12, sub {print "left text: $text\n"}, -side => 'left' ); my $updateright = verticalentry( $middle, \$entry, 12, sub {print "right text: $entry\n"}, -side => 'right' ); $middle->Frame(-width => 30)->pack; $mw->Button(-text => 'Exit', -command => sub {$mw->destroy}, )->pack( -side => 'bottom', -fill => 'x' ); $mw->Button(-text => 'Clear Right', -command => sub {$updateright->('' +)}, )->pack( -side => 'bottom', -fill => 'x' ); $mw->repeat( 400, sub { -M $0 < 0 and $mw->destroy } ); # file saved a +fter run MainLoop; -M $0 < 0 and exec $0; # restart on editor save :) sub verticalentry { my ($parent, $textref, $length, $command, @packargs) = @_; my $edit = $parent->Label(-width => 1, -relief => 'sunken', -borderwidth => 3, -font => 'courierbold 30')->pack( @packargs ); my $update = sub { @_ and $$textref = shift; $edit->configure(-text => join "\n", split //, substr $$textref . ' ' x $length, 0, $length); }; $update->(); $edit->bind('<Enter>' => sub { $edit->focus } ); $edit->bind('<Key>' => sub { my $key = $_[0]->XEvent->A; use Data::Dump 'dd'; dd $key; if( $key =~ /^[ -~]$/ ) { length $$textref < $length and $$textref .= $key; } elsif( $key eq "\b" ) { chop $$textref; } elsif( $key eq "\r" ) { $command->(); } $update->(); } ); return $update; }

    If nothing else, it shows a way to get a vertical column of text :)

    This example has two independent vertical entries just so I could be sure all the closure stuff worked correctly.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11113817]
Approved by kcott
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (1)
As of 2024-04-25 04:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found