Help tkperl: How to select entry widget 
Author Message
 Help tkperl: How to select entry widget

I am working on a widget that contains several "LabeledEntry"
widgets for text entry. I've been struggling for hours trying
to have the <Return> button bound (tkbind) to select the
next entry (Have the text cursor and the focus in the next field).

Up to now the only way to enter text in a field is to click
in it with <Button-1>.

The code I'm using is:

# dbpDialog - an adaptation from tkDialog for entries instead of buttons.
# TkDialog - a translation of `tk_dialog' from Tcl/Tk to TkPerl (based on John Stoffel's idea).
# TkDialog - Stephen O. Lidie, Lehigh University Computing Center.  94/11/27
#
# Patrick Groeneveld, Advanced Computer Research Institute. 26/01/95

#
#
# 1) Call the constructor to create the dialog object, which in turn returns a blessed reference to the new object:
#
#    use dbpDialog;
#
#    Initialize the path to your dialog window in the Constructor (sub new)
#    or in the main if not in a widget.

#
#       w              - Window pathname to use for the dialog's Toplevel widget, which must be unique.
#       title          - Title to display in the dialog's decorative frame.
#       text           - Message to display in the dialog widget.
#       entry_labels   - One or more strings to display in front of the text entry areas.
#
# 2.a) Envoke the dbpDialog method from a widget:
#

#
# 2.b) Envoke the dbpDialog method from a sub routine:
#   At the end of your Constructor return a blessed reference to dialWin
#       return bless({dw => $dialWin});
#
#   From the subroutine (Private Method)


package dbpDialog;

require 5.000;
require Exporter;
use Carp;
use English;
use Tk;
use TkEnglish;
use LabeledEntry;


$top = "";

###################
# Public Method
###################

####################
# Constructor
####################

sub new {

    # dbpDialog object constructor - create the dialog window.


    my($i, $w_top, $w_bot, $w_msg, $winpar, $winvx, $winvy, $winrw, $winrh, $winsw, $winsh, $eln, $next);


    # Create the Toplevel window and divide it into top mid and bottom parts.

    # Get longest entry_label
    $i = 0; $eln = 0;

        if (length($_) < $eln) {next}
        $eln = length($_);
    }

    $selected_button = "";
    $top = Toplevel::new $path; # "-class" => "Dialog");
    wm "title", $top, "$title";
    wm "iconname", $top, "$title";

    $w_top = Frame::new $top, Relief => "raised", Borderwidth => 1;
    $w_mid = Frame::new $top, Relief => "raised", Borderwidth => 1;
    $w_bot = Frame::new $top, Relief => "raised", Borderwidth => 1;
    tkpack $w_top, "-side" => "top", "-fill" => "both", "-expand" => "yes";
    tkpack $w_mid, "-side" => "top", "-fill" => "both", "-expand" => "yes";
    tkpack $w_bot, "-side" => "top", "-fill" => "both", "-expand" => "yes";

    # top part with the help button on the right

    tkpack(Button::new($w_top,
                "-text" => "Help",
                "-width" => 6,
                "-method" => sub { print "Help yourself!\n"}),
                "-side" => "right",
                "-anchor" => "e");
    tkpack(Label::new($w_top,
                "-text" => "$msgtxt"),
                "-side" => "left",
                "-anchor" => "w");

    # Create a column of text entries in the middle of the dialog.

    $j = 0;

        $w_lab[$j] = LabeledEntry::new($w_mid, $_, $eln);
        tkpack $w_lab[$j], "-in" => $w_mid, "-side" => "top", "-expand" => "yes", "-fill" => "x";
    }

    # Create OK and Cancel buttons at the bottom of the dialog.

  tkpack(Button::new($w_bot,
                     -text => "Cancel",
                     -method => "dbpDialog::cancl"),
         "-side" => "right",
         "-expand" => "no",
         "-fill" => "x",
         "-padx" => "2",
         "-pady" => "2",
         );

  tkpack(Button::new($w_bot,
                     -text => "OK",
                     -width => 6,
                     -method => "dbpDialog::itsok"),
         "-side" => "left",
         "-expand" => "no",
         "-fill" => "x",
         "-padx" => "2",
         "-pady" => "2",
         );

    # Withdraw the window, update all geometry information, center it in the display and return a reference to the dialog.

    wm "withdraw", $top;
    idletasks;
    ###$winpar = tclcmd "winfo", "parent", $top;
    $winvx = tclcmd "winfo", "vrootx", $top; ### $winpar
    $winvy = tclcmd "winfo", "vrooty", $top; ### $winpar
    $winrw = tclcmd "winfo", "reqwidth", $top;
    $winrh = tclcmd "winfo", "reqheight", $top;
    $winsw = tclcmd "winfo", "screenwidth", $top;
    $winsh = tclcmd "winfo", "screenheight", $top;
    my $x = int($winsw/2 - $winrw/2 - $winvx);
    my $y = int($winsh/2 - $winrh/2 - $winvy);
    wm "geom", $top, "+$x+$y";

    return bless({Default => $top, Lastentry => $w_lab[$j], Numentries => $j});

Quote:
} # end dbpDialog constructor

######################
# Private Method
######################

sub showDial {
    $w = shift;
    $done = "";

    wm "deiconify", $w;
    waitvar $done;

Quote:
}

sub itsok {


        $_->tkdelete(0, "end");
    }
    wm("withdraw", $w);
    $done = "GO";

Quote:
}

sub cancl {

        $_->tkdelete(0, "end");
    }
    wm("withdraw", $w);
    $done = "KO";

Quote:
}

1;

#-------------------######################-------------------------
#                   LabeledEntry.pm
#-------------------######################-------------------------

# LabeledEntry.pm - an Entry Widget with a Label to the left.
#
# Author:   John Stoffel

# Date:     11/23/1994
# Version:  1.0
#

# Added a scrollbar on top of the entry
# Date: 25/01/96
#
# usage:
#
# use LabeledEntry;
# $LE = LabeledEntry::new($top,"Label", "width");
#

package LabeledEntry;

sub new {
  local($top) = shift;
  local($text) = shift;
  local($wth) = shift;
  my($frame,$label,$entry,$s);

  $frame = Frame::new($top,
                      "-relief" => "ridge",
                      "-width" => 5,
                      "-borderwidth" => 3,
                      );

  $s = Scrollbar::new($frame, "-orient" => "horizontal", "-width" => 8);
  $entry = Entry::new($frame,
                      "-background" => "PeachPuff",
                      "-relief" => "sunken",
                      "-width"        => 50,
                      "-slave"        => $s,
                      "-method"       => "set");

  $s->configure("-slave"  => $entry,
                      "-method" => "view");

  $label = Label::new($frame,
                      "-text" => $text,
                      "-width" => $wth,
                      );

  Tk::tkpack($label,
             "-side" => "left",
             );

  Tk::tkpack( $s,
               "-side" => "top",
               "-fill" => "x",
               "-expand" => "yes",
               "-pady" => 2,
               );

  Tk::tkpack( $entry,
               "-side" => "left",
               "-fill" => "x",
               "-expand" => "yes",
               "-pady" => 2,
               );

  return bless({Entry => $entry,
                Label => $label,
                Default => $frame,
              });

Quote:
}

1;

#-------------------######################-------------------------



Wed, 16 Jul 1997 02:50:05 GMT  
 Help tkperl: How to select entry widget

Quote:

> I am working on a widget that contains several "LabeledEntry"
> widgets for text entry. I've been struggling for hours trying
> to have the <Return> button bound (tkbind) to select the
> next entry (Have the text cursor and the focus in the next field).

> Up to now the only way to enter text in a field is to click
> in it with <Button-1>.

> The code I'm using is:
> .......

Well it took some more time, but I finally made it!

Hints: (Since this is my first programing experience with Perl and
------  with Tk, I did the following:)

1) Read again and again the tkperl GUIDE
2) Keep Llama and Camel books close to the keyboard
3) Read Tk man pages
4) Take a close look at Tk.pm
5) Study carefully existing programs and examples

Comments:
---------
I've studied some tkperl programs and noticed that there are a lot
of different ways to write a perl5 program. Being completely new
to both Perl and Object Oriented programing I've been puzzled a lot
and starting to write my own programs wasn't easy even if I cheated
by starting from existing programs.
While reading my code you'll find a lot of inconsistencies due to that.

Results:
--------
dbpDialog.pm :  Dialog widget with variable number of Radio buttons
                and variable number of Entry widgets.
                Pressing <Return> or <Tab> in one of the entry selects
                the next Entry and places the insertion cursor at the end
                of the string if there's one.
                Note: To know all the possible operations in a Entry widget
                read the Tk "entry" man pages.

LabeledEntry.pm: Got it from this newsgroup, but in order to work with
                dbpDialog I added a $pos variable, which I return blessed
                it's an index that I use to know the position of the widget
                at creation time.
                For my own convenience, I added a scrollbar on top of the
                Entry widget it's linked to (At that time I did not know that
                the middle mouse button could scroll the entry, but now I
                keep it since it helps to know the relative position of the
                cursor in the string).

####################################### 1. dbpDialog.pm #########################

# dbpDialog - an adaptation from tkDialog for entries instead of buttons.
# TkDialog - a translation of `tk_dialog' from Tcl/Tk to TkPerl (based on John Stoffel's idea).
# TkDialog - Stephen O. Lidie, Lehigh University Computing Center.  94/11/27
#
# Patrick Groeneveld, Advanced Computer Research Institute. 26/01/95

#
#
# 1) Call the constructor to create the dialog object, which in turn returns a blessed reference to the new object:
#
#    use dbpDialog;
#
#    Initialize the path to your dialog window in the Constructor (sub new)
#    or in the main if not in a widget.

#
#       w              - Window pathname to use for the dialog's Toplevel widget, which must be unique.
#       title          - Title to display in the dialog's decorative frame.
#       text1           - Message to display in the dialog widget.
#       text2           - Message to display in the radio buttons frame.
#       n1             - The length of the radio button list.
#       radio_labels   - One or more strings to display with radio buttons.
#       entry_labels   - One or more strings to display in front of the text entry areas.
#
# 2.a) Envoke the dbpDialog method from a widget:
#

#
# 2.b) Envoke the dbpDialog method from a sub routine:
#   At the end of your Constructor return a blessed reference to dialWin
#       return bless({dw => $dialWin});
#
#   From the subroutine (Private Method)


package dbpDialog;

require 5.000;
require Exporter;
use Carp;
use English;
use Tk;
use TkEnglish;
use LabeledEntry;


$top = "";

###################
# Public Method
###################

####################
# Constructor
####################

sub new {

    # dbpDialog object constructor - create the dialog window.


    my($i, $w_top, $w_bot, $w_msg, $winpar, $winvx, $winvy, $winrw, $winrh, $winsw, $winsh, $eln, $next);
    croak "The length of the radio buttons list is greater than expected" if $lrad > $#labels;



    $i = 0;


        $i++
    }


    # Create the Toplevel window and divide it into top mid and bottom parts.

    # Get longest entry_label
    $i = 0; $eln = 0;

        if (length($_) < $eln) {next}
        $eln = length($_);
    }

    $selected_button = "";
    $top = Toplevel::new $path; # "-class" => "Dialog");
    wm "title", $top, "$title";
    wm "iconname", $top, "$title";

    $w_top = Frame::new $top, Relief => "raised", Borderwidth => 1;
    $w_topmid = Frame::new $top, Relief => "sunken", Borderwidth => 1;
    $w_mid = Frame::new $top, Relief => "raised", Borderwidth => 1;
    $w_bot = Frame::new $top, Relief => "raised", Borderwidth => 1;
    tkpack $w_top, "-side" => "top", "-fill" => "both", "-expand" => "yes";
    tkpack $w_topmid, "-side" => "top", "-fill" => "both", "-expand" => "yes";
    tkpack $w_mid, "-side" => "top", "-fill" => "both", "-expand" => "yes";
    tkpack $w_bot, "-side" => "top", "-fill" => "both", "-expand" => "yes";

    # top part with the help button on the right

    tkpack(Button::new($w_top,
                "-text" => "Help",
                "-width" => 6,
                "-method" => sub { print "Help yourself!\n"}),
                "-side" => "right",
                "-anchor" => "e");
    tkpack(Label::new($w_top,
                "-text" => "$msgtxt",
                "-foreground" => "red"),
                "-side" => "left",
                "-anchor" => "w");

    # Create a row of radio buttons at the top of the middle frame.

    tkpack(Label::new($w_topmid,
                "-text" => "$radtxt"),
                "-side" => "left",
                "-anchor" => "w");

    $i = 0; $radioVar = $radio_labels[0];

        $w_rad[$i] = Radiobutton::new($w_topmid, "-text" => $_,
                                              "-variable" => $radioVar,
                                              "-value" => $_);
        tkpack $w_rad[$i],"-in" => $w_topmid, "-side" => "left",
                "-padx" => 2, "-pady" => 2, "-expand" => "no";
        $i++;
    }

    # Create a column of text entries in the middle frame of the dialog.

    $j = 0;

        $w_lab[$j] = LabeledEntry::new($w_mid, $_, $eln);
        tkpack $w_lab[$j], "-in" => $w_mid, "-side" => "top", "-expand" => "yes", "-fill" => "x";
        $w_lab[$j]->{"Pos"} = $j;
        tkbind($w_lab[$j]->{"Entry"}, "<Return>", sub { focusNext() });
        tkbind($w_lab[$j]->{"Entry"}, "<Tab>", sub { focusNext() });
        $j++;
    }

    # Create OK and Cancel buttons at the bottom of the dialog.

  tkpack(Button::new($w_bot,
                     -text => "Cancel",
                     -method => "dbpDialog::cancl"),
         "-side" => "right",
         "-expand" => "no",
         "-fill" => "x",
         "-padx" => "2",
         "-pady" => "2",
         );

  tkpack(Button::new($w_bot,
                     -text => "OK",
                     -width => 6,
                     -method => "dbpDialog::itsok"),
         "-side" => "left",
         "-expand" => "no",
         "-fill" => "x",
         "-padx" => "2",
         "-pady" => "2",
         );

#           tkbind $top, "<Return>" => sub {$w_but->flash; $selected_button = $ARG[0]}, $i;

    # Withdraw the window, update all geometry information, center it in the display and return a reference to the dialog.

    wm "withdraw", $top;
    idletasks;
    ###$winpar = tclcmd "winfo", "parent", $top;
    $winvx = tclcmd "winfo", "vrootx", $top; ### $winpar
    $winvy = tclcmd "winfo", "vrooty", $top; ### $winpar
    $winrw = tclcmd "winfo", "reqwidth", $top;
    $winrh = tclcmd "winfo", "reqheight", $top;
    $winsw = tclcmd "winfo", "screenwidth", $top;
    $winsh = tclcmd "winfo", "screenheight", $top;
    my $x = int($winsw/2 - $winrw/2 - $winvx);
    my $y = int($winsh/2 - $winrh/2 - $winvy);
    wm "geom", $top, "+$x+$y";

    return bless({Default => $top});

Quote:
} # end dbpDialog constructor

######################
# Private Method
######################

sub showDial {
    $w = shift;
    $done = "";

    wm "deiconify", $w;
    waitvar $done;

Quote:
}

sub itsok {



        $_->tkdelete(0, "end");
    }

    wm("withdraw", $w);
    $done = "GO";

Quote:
}

sub cancl {
  local($curw);

        $curw = $_;
        $curw->{"Entry"}->tkdelete(0, "end");
    }
    wm("withdraw", $w);
    $done = "KO";

Quote:
}

sub focusNext {
    local($curw) = $Tk::EvW;




        if ($parentPath eq path($_)) {$curw = $_; last}
    }
    local($pos) = $curw->{"Pos"};
    ($pos >= $j-1) ? $pos = 0 : $pos++;
    $curw = $w_lab[$pos];
    local($e) = $curw->{"Entry"};
    $e->icursor("end");
    $e->selectfrom("end");
    focus($e) if ($e->configure("-state"))[4] eq "normal";

Quote:
}

1;

####################################### 1. LabeledEntry.pm #########################

# LabeledEntry.pm - an Entry Widget with a Label to the left.
#
# Author:   John Stoffel

# Date:     11/23/1994
# Version:  1.0
#

# Added a scrollbar on top of the entry
# Date: 25/01/96
#
# usage:
#
# use LabeledEntry;
# $LE = LabeledEntry::new($top,"Label", "width");
#

package LabeledEntry;

sub new {
  $top = shift;
  local($text) = shift;
  local($wth) = shift;
  my($frame,$label,$entry);

# Added $pos for position control in dbpDialog.pm
  my($pos) = 0;

  $frame = Frame::new($top,
                      "-relief" => "ridge",
...

read more »



Tue, 22 Jul 1997 01:58:07 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. entry widget in tkperl-0.029

2. !Help with Entry widget

3. help with entry widget

4. TkPerl : Entry::newbie question

5. tkperl: Questions on buttons and squeaking widgets

6. Text Widget for tkperl

7. TkPerl blt barchart widget program

8. Is there a terminal widget for tkperl?

9. TkPerl: Executable output to Text Widget

10. TKPerl: Postscript method for canvas widget

11. TkPerl Radiobutton widget

12. Paned Window Widget in Tkperl

 

 
Powered by phpBB® Forum Software