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

need a popup gui stdin

by diamondsandperls (Beadle)
on May 24, 2012 at 20:15 UTC ( [id://972309]=perlquestion: print w/replies, xml ) Need Help??

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

I have browsed sample code online, but have not found anything of use. I need a popup stdin that is GUI based where the user clicks ok and it stores the value typically stdin I do the below.

my $variable = <STDIN>; chomp $variable;


:)

Replies are listed 'Best First'.
Re: need a popup gui stdin
by zentara (Archbishop) on May 24, 2012 at 21:11 UTC
    Here is a little fancier one for you, with window centering, like a real popup.
    #!/usr/bin/perl use warnings; use strict; use Tk; my $MainWindow = MainWindow->new; $MainWindow->title("Data Entry Form"); # hide window to prevent a double flicker # it is raised later after it is built $MainWindow->withdraw; $MainWindow->fontCreate('big', -family=>'arial', -weight=>'bold', -size=> 18 ); my $NameOfPerson; # global variable $MainWindow -> Label(-justify => 'left', -bg => 'white', -font => 'big', -text => "Name of person : ") ->pack(-side => 'left',-anchor => 'n'); my $entry = $MainWindow -> Entry( -bg => 'white', -font => 'big', -selectborderwidth => 10) ->pack(-side => 'top',-anchor => 'n'); $entry->bind('<Return>',[\&somesub]); $entry->focus; $MainWindow -> Button(-text => "OK", -command => \&somesub) ->pack(-side => 'bottom',-anchor => 'center'); #centers window for popup #CenterWindow($MainWindow, 300, 200); CenterWindow($MainWindow); # show window now $MainWindow->deiconify(); $MainWindow->raise; MainLoop; #your script can continue here after Tk is done # ie after Mainwindow destroy is run while(1){print time."\t$NameOfPerson Your script continues here\n"; sl +eep 1;} sub somesub { $,="\n"; $NameOfPerson = $entry -> get; print "\nNameOfPerson ->$NameOfPerson\n"; $MainWindow -> destroy; } sub CenterWindow { my($window, $width, $height) = @_; $window->idletasks; $width = $window->reqwidth unless $width; $height = $window->reqheight unless $height; my $x = int(($window->screenwidth / 2) - ($width / 2)); my $y = int(($window->screenheight / 2) - ($height / 2)); $window->geometry("=${width}x${height}+${x}+${y}"); }

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

      errors on the below line Can't call method "setgrid" on an undefined value at

      $lab_pass -> grid(-row=>2,-column=>1);
        errors on the below line ...
         $lab_pass -> grid(-row=>2,-column=>1);

        This is code from jack123's reply, not zentara's. Try fixing the former code – it can be fixed – and running it; it may throw light on what you need. Or else try zentara's code, with which I have not experimented, but in which I would have more confidence.

Re: need a popup gui stdin
by jack123 (Acolyte) on May 24, 2012 at 20:22 UTC
    For creating GUI application first refer : Perl/Tk or cpan perl tk A sample code :
    Use Tk; my $mw = new MainWindow(-background=>'#3B5998'); my $frm_name = $mw -> Frame(); my $lab = $frm_name -> Label(-text=>"Enter your feedback"); my $ent = $frm_name -> Entry(); my $but = $mw -> Button(-text=>"Submit", -command =>\&push_button); $lab -> grid(-row=>2,-column=>1); $ent -> grid(-row=>2,-column=>2); $frm -> grid(-row=>2,-column=>1,-columnspan=>2); $but -> grid(-row=>4,-column=>1,-columnspan=>2); MainLoop; sub push_button { my $txt=$ent -> get(); $mw -> messageBox(-type=>"ok", -message=>"You printed this $txt"); }
    Updated my code, it is just a sample program and also don't forget to install TK and dependencies related with it.
      Updated my code, it is just a sample program...

      The updated code still won't work. The statement
          Use Tk;
      is still wrong and the  $frm scalar is nowhere defined or, more importantly, initialized. See the statement
          $frm -> grid(-row=>2,-column=>1,-columnspan=>2);

      Even sample programs should actually work, especially when they are being given to beginners.

      Also, can you make the program run with strictures and warnings enabled?
          use warnings;
          use strict;

Re: need a popup gui stdin
by xiaoyafeng (Deacon) on May 29, 2012 at 09:25 UTC
    To be honest, I don't think teaching newbie Tk is a good idea.
    1. If platform is Linux, using Gtk2 coz of Glade.
    2. if platform is Win$, using Wx coz Wxdesigner
    3. or you can even use C# form with a built-in perl interpreter.
    4. My experience is:

      Never use a GUI module without a usable RAD tool, unless you are sure you would use it often in next 10 years. Coz you have to invest many in this module to be familiar with its grammar, tricks or other details. you can do such effort on html/css, coz it seems not to be dead in next 5 years.





      I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

      To be honest, I don't think teaching newbie Tk is a good idea..... Never use a GUI module without a usable RAD tool,

      I think your advice is bad. I think YOU want to avoid learning the true nature of GUI programming, so you need a RAD tool, so you advise this to everyone else. You are just lazy.

      Anyone with any knowledge of GUI's will tell you you are better off avoiding RAD tools because of the incomprehensible boilerplate code it produces. Try debugging a difficult glitch in boilerplate code, and you will wish a real programmer wrote the code with sensible variable names.

      As for teaching newbies, Tk's documentation, tutorials and books far surpass anything Wx has, not to mention the rediculous object-oriented layer which Wx imposes on you, just to pop open a few windows.


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

        not to mention the rediculous object-oriented layer which Wx imposes on you, just to pop open a few windows.

        :| It sure beats Tk.pm's under used and cuseless object-oriented layer -- thats why all your programs are closure closure closure :D

        Also, you responded to me not xiaoyafeng :)

Log In?
Username:
Password:

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

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

    No recent polls found