Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

getting entered data from a pTk megawidget

by Anonymous Monk
on Jan 10, 2022 at 21:03 UTC ( [id://11140345]=perlquestion: print w/replies, xml ) Need Help??

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

How do I tell PERL to get the data that was entered into a megawidget back so that it can be used in the program that is calling the megawidget? Is there a connection between the way the megawidget is coded, and how the data is called/extracted?

#!/usr/bin/perl use strict; use warnings; use Tk; { # for package package Tk::GUIask; # Pop up a box asking for simple inpu +t: # filename, dir, etc use List::Util qw( first ); use base qw/ Tk::Frame /; # Frame-based composite Construct Tk::Widget 'GUIask'; # install MyNewWidget in pTk namespac +e sub ClassInit # called once to initialize new class { my($class, $mw) = @_; $class->SUPER::ClassInit($mw); } sub Populate # called to build each widget instanc +e { my($self, $args) = @_; $self->SUPER::Populate($args); #my $search = ''; my $frame = $self->Frame( -borderwidth => 5, -relief => 'ridge', )->pack(-fill=> 'both', -expand=> 1); my $label = $frame->Label( -fg => 'blue', -font => 30, )->pack(-fill => 'x'); my $search; my $entry = $frame->Entry( -textvariable => \$search, -validate => 'key', # -validatecommand => sub # { # my ($want) = +@_; # length $want +or return 1; # my @list = $l +box->get(0 , "end"); # my $item = fi +rst { $list[$_] =~ /^\Q$want\E/ } 0 .. $#list; # defined $item + or return 0; # $lbox->select +ionClear( 0 , "end" ); # $lbox->select +ionSet($item); # $lbox->see($i +tem); # 1 # to allow # }, )->pack(-fill => 'x'); my $button = $frame -> Button( -text => "Abort", -bg => 'red', -command => sub { #$mw -> destroy(); ($frame -> destroy() ) if Tk::Exists +($frame ); } ) ->pack(-fill => 'x' ); $self->ConfigSpecs( DEFAULT => [$entry], text => [$label] ); $self->Delegates( Construct => $entry, insert => $entry, get => $entry, curselection => $entry ); } 1; } # CLOSE package my $header_msg = "ENTER Archive name: "; my $mw = MainWindow->new; #$mw->geometry( '+900+250' ); $mw->title( ' ' ## " " must be specified for a plain frame ); my $search; my $lb = $mw->GUIask( -text => $header_msg, -textvariable => \$search, )->pack(-fill => 'both', -expand => 1, -side => 'left'); my $stuff = $lb -> get(); # print "\$search =: $stuff\n\n "; MainLoop;

Replies are listed 'Best First'.
Re: getting entered data from a pTk megawidget
by kcott (Archbishop) on Jan 10, 2022 at 21:54 UTC

    Your Tk::GUIask module should be in a separate .pm file so that it can be reused.

    It looks like you've just reinvented Tk::LabEntry. If you really want to create a new widget, perhaps as an academic exercise, see the Tk::LabEntry code for pointers on how to proceed.

    The basic idea is to advertise your Tk::Entry subwidget. In your code that uses Tk::GUIask, access the Tk::Entry subwidget, then use it as you would a plain Tk::Entry widget.

    See also: Tk::mega, Tk::composite, and other documentation linked from those, such as Tk::ConfigSpecs and Tk::Derived. You may also find the "Sample Perl Mega-Widgets" section of the Widget Demo to be useful.

    — Ken

      I think I looked at Tk::LabEntry -- and yes I am basically rewriting it. But I want it done MY WAY. It must have a programmer-setable message; and it must have an 'Abort' or 'Quit' button. I have done this several times in a program I am writing. I want to convert this block of code into a megawidget to avoid rewriting this code repeatedly. Building the widget appears easy -- getting the input back so the application can use it is a pain...

        getting the input back so the application can use it is a pain...

        so you're saying delegates isnt working for get method? Strip all other parts if program until you get get delegates working...

        im on mobile

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-24 05:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found