Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

tk how to create entry box from menu selection

by gibsonca (Beadle)
on Feb 02, 2010 at 15:04 UTC ( #820955=perlquestion: print w/ replies, xml ) Need Help??
gibsonca has asked for the wisdom of the Perl Monks concerning the following question:

How do I implement a dynamic text entry box from a menu choice? For example, after the user selects Options -> Build, an entry box should appear, sorta like a dialog box for files, so that the user can enter text. The goal is to NOT have the text message box in the GUI. Once the data is entered, the text box would no longer be visible. Could be on a 'return' or an 'exit' button. I believe the right code would go where the "### ? ###" is in the following - Typical menu (original source from www.bin-co.com) :

#!/usr/local/bin/perl use Tk; # Main Window my $mw = new MainWindow; #Making a text area my $txt = $mw -> Scrolled('Text',-width => 50,-scrollbars=>'e') -> pac +k (); #Declare that there is a menu my $mbar = $mw -> Menu(); $mw -> configure(-menu => $mbar); # The Main Buttons my $file = $mbar -> cascade(-label=>"File", -underline=>0, -t +earoff => 0); my $others = $mbar -> cascade(-label =>"Others", -underline=>0, -t +earoff => 0); my $options = $mbar -> cascade(-label => "Options", -underline=>0, -t +earoff => 0); my $help = $mbar -> cascade(-label =>"Help", -underline=>0, -t +earoff => 0); ## File Menu ## $file -> command(-label => "New", -underline=>0, -command=>sub { $txt -> delete('1.0','end');} ); $file -> checkbutton(-label =>"Open", -underline => 0, -command => [\&menuClicked, "Open"]); $file -> command(-label =>"Save", -underline => 0, -command => [\&menuClicked, "Save"]); $file -> separator(); $file -> command(-label =>"Exit", -underline => 1, -command => sub { exit } ); ## Options Menu ## $options -> command(-label =>"Build", -underline=> 0, -command => \&options ); ## Others Menu ## my $insert = $others -> cascade(-label =>"Insert", -underline => 0, -t +earoff => 0); $insert -> command(-label =>"Name", -command => sub { $txt->insert('end',"Name : Binny V A\n");}); $insert -> command(-label =>"Website", -command=>sub { $txt->insert('end',"Website : http://www.geocities.com/binnyva/\n" +);}); $insert -> command(-label =>"Email", -command=> sub {$txt->insert('end',"E-Mail : binnyva\@hotmail.com\ +n");}); $others -> command(-label =>"Insert All", -underline => 7, -command => sub { $txt->insert('end',"Name : Binny V A Website : http://www.geocities.com/binnyva/ E-Mail : binnyva\@hotmail.com"); }); ## Help ## $help -> command(-label =>"About", -command => sub { $txt->delete('1.0','end'); $txt->insert('end', "About ---------- This script was created to make a menu for a\nPerl/Tk tutorial. Made by Binny V A Website : http://www.geocities.com/binnyva/code E-Mail : binnyva\@hotmail.com"); }); MainLoop; # ----------- sub options { # ----------- $gBuild = "TBD"; ### ? ### print "options; build is $gBuild\n"; } # end of options() sub menuClicked { my ($opt) = @_; $mw->messageBox(-message=>"You have clicked $opt. This function is not implanted yet."); }

Any help appreciated.

Comment on tk how to create entry box from menu selection
Download Code
Re: tk how to create entry box from menu selection
by keszler (Priest) on Feb 02, 2010 at 15:18 UTC
    You're looking for a popup dialog box with an Entry field? Try Tk::EntryDialog.

      Sounds like an excellent suggestion, but I can not seem to install it on MS windows. I will review the instructions again...

        It's possible - likely even - that ActiveState has not created a PPM package for it. There are other package repositories; see http://win32.perl.org/wiki/index.php?title=PPM_Repositories for a good list.

        If none of the repositories has a package, you can download the tarball from CPAN. It's a pure Perl module, so you don't need a compiler. The standard

        > perl Makefile.PL > nmake > nmake test > nmake install
        should work fine.
Re: tk how to create entry box from menu selection
by gibsonca (Beadle) on Feb 02, 2010 at 17:55 UTC

    I found a snippet of code for username pw, so then my solution was trivial:

    # ----------- sub options { # ----------- my $tBuild; require Tk::LabEntry; require Tk::DialogBox; $options = $mw->DialogBox(-title => 'Build Version', -buttons => ['Ok', 'Cancel'], -default_button => 'Ok'); $options->add('LabEntry', -textvariable => \$tBuild, -width => 5, -label => 'Enter build version:', -labelPack => [-side => 'left'])->pack; my $sts = $options->Show(); if ($sts eq "Ok") { $gBuild = $tBuild; } $txt->insert('end',"\n Build version set to : '$gBuild'"); } # end of options()

    I still need to figure out color, position and why I need the require's..

Re: tk how to create entry box from menu selection
by zentara (Archbishop) on Feb 02, 2010 at 18:02 UTC
    This works on linux
    # ----------- sub options { # ----------- $gBuild = "TBD"; ### ? ### print "options; build is $gBuild\n"; # code added by me ###################### use Tk::DialogBox; my $dialog = $mw->DialogBox( -buttons => [qw/Ok Cancel/], -title => "Enter New Value" ); my $dialogE = $dialog->add("Entry"); $dialogE->pack(qw/-padx 10 -pady 10/); ## Clear the Entry before showing the dialog $dialogE->delete( 0, 'end' ); ## Determine whether or not the user hit "Ok" my $button = $dialog->Show(); if ( $button eq "Ok" ) { my $letters = $dialogE->get(); print "$letters was submitted\n"; } ######################################## } # end of options()

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2014-10-02 03:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (45 votes), past polls