Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

A very strange problem with entry, button widgets along with waitvariable(\$X)

by rjohn1 (Sexton)
on Nov 15, 2013 at 04:15 UTC ( [id://1062698]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks, The below non-working code just asks for the user entry, waiting for it. Now there is a very strange problem i face as described below.

sub pop_user_entry{ $temp_var = ""; my $user_entry_done = 0 ; my $dialog_top = $mw->Toplevel(-title=>"User Entry"); $dialog_top->Entry(-textvariable=>\$temp_var)->pack; $dialog_top->Button(-text=>"OK",-command=>sub {$user_entry_don +e = 1 unless($temp_var =~ m/^$/);})->pack; $dialog_top->waitVisibility; $dialog_top->grab; $dialog_top->waitVariable(\$user_entry_done); $dialog_top->grabRelease ; $dialog_top->destroy; }

The problem is that when pop_user_entry is called, i CANNOT enter into the Entry widget via keyboard. But strangely if i pack the Button before Entry, then all works fine. Working Code:

$dialog_top->Button(-text=>"OK",-command=>sub {$user_entry_done = 1 un +less($temp_var =~ m/^$/);})->pack; $dialog_top->Entry(-textvariable=>\$temp_var)->pack;
But because of common sense, i want the Entry before Button as shown in original code which does not work. Please let me know if you can spot the problem in the original code:

Replies are listed 'Best First'.
Re: A very strange problem with entry, button widgets along with waitvariable(\$X)
by Anonymous Monk on Nov 15, 2013 at 05:29 UTC
    One problem is my $dialog_top = $mw->Toplevel, dialog is spelled $mw->Dialog, see Tk::Dialog
Re: A very strange problem with entry, button widgets along with waitvariable(\$X)
by kcott (Archbishop) on Nov 16, 2013 at 18:46 UTC

    G'day rjohn1,

    I suspect your problem lies with the declaration of $temp_var (or possibly lack of declaration). The absence of arguments to pop_user_entry() may also be an issue. You haven't shown enough code for me to tell.

    Please supply a short working script. It should include the strict and warnings pragmata and, obviously, show the call to pop_user_entry(). It only needs code relevant to the problem at hand: please do not include anything beyond this. Also provide verbatim copies of any warning or error messages you're seeing.

    If you do this, I'll happily look at the problem.

    -- Ken

      Hi Ken, I am putting the header code here. This is just temp code i created to post here.

      use Tk; use warnings; use strict; use Getopt::Long; use Cwd; use List::Util qw<first>; require Tk::ROText; my $temp_var = ""; my $mw = MainWindow->new(); $mw->geometry("1200x1200+0+680"); $mw->title("SVN Util"); $mw->resizable(0,1); $mw->Button(-text=>"Test", -command=>\&pop_user_entry)->pack; MainLoop; sub pop_user_entry{ $temp_var = ""; my $user_entry_done = 0 ; my $dialog_top = $mw->Toplevel(-title=>"User Entry"); $dialog_top->Entry(-textvariable=>\$temp_var)->pack; $dialog_top->Button(-text=>"OK",-command=>sub {$user_entry_don +e = 1 unless($temp_var =~ m/^$/);})->pack; $dialog_top->waitVisibility; $dialog_top->grab; $dialog_top->waitVariable(\$user_entry_done); $dialog_top->grabRelease ; $dialog_top->destroy; }

      As i said if you flip the button and entry widget declaration in the method definition, the entry widget takes the keyboard input.

        To be frank, I'm not overly happy with this. I asked:

        "Please supply a short working script."

        The code you've supplied does not even compile.

        I also asked:

        "Also provide verbatim copies of any warning or error messages you're seeing."

        So, where is:

        Global symbol "$user_entry_don" requires explicit package name at ... Execution of ... aborted due to compilation errors.

        Would I be right in assuming that you didn't even bother to test this code?

        In addition, I asked you to exclude any code that wasn't "relevant to the problem at hand". You've included four modules not used by the code you posted (Getopt::Long, Cwd, List::Util and Tk::ROText), a geometry() call (only suitable for particularly large screens) and other code: all irrelevant!

        I changed:

        $dialog_top->Button(-text=>"OK",-command=>sub {$user_entry_don +e = 1 unless($temp_var =~ m/^$/);})->pack;

        to:

        $dialog_top->Button(-text=>"OK",-command=>sub {$user_entry_don +e = 1 unless($temp_var =~ m/^$/);})->pack;

        The script now compiles and runs without any error or warning output. The "Test" button pops up the Toplevel; I'm able to enter text from the keyboard in the Entry widget; the "OK" button removes the Toplevel.

        I swapped the order of the Entry and Button widget code. Other than those widgets appearing in reverse order, the script runs the same as before.

        I am unable to reproduce your problem.

        I'm using Perl v5.18.1 and Tk v804.031 — perhaps try upgrading if you're using older versions.

        -- Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-03-28 15:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found