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

Few doubts in perl-tk.

by gopalr (Priest)
on Jan 22, 2005 at 06:56 UTC ( [id://424198]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

I have few doubts in Perl Tk.

1. how to limit the no. of characters to type in an entry widget?

2. Remove the tk icon in the mainwindow and also in toplevel window

Thanks in advance

Considered by spurperl - change "doubts on/in" to "questions about"
Unconsidered by castaway - Keep/Edit/Delete: 13/14/0 - no real reason to change

Replies are listed 'Best First'.
Re: Few doubts in perl-tk.
by Koosemose (Pilgrim) on Jan 22, 2005 at 07:35 UTC

    To answer your first question, Take a look at the documentation for Tk::Entry, look for the options called, -invalidcommand, -validatecommand, and -validate. Also there's a section in there titled VALIDATION which should help.

    As luck would happen I happen to have just been working on a Tk script and validating one of my entry widgets, so I can provide some sample code to give you a loose idea of what you might want to do

    my $minEntry = $right->Entry( -text => '0', -width => 3, -validate => 'key', -validatecommand => sub { $proposedtext = shift; length($proposedtext) <= 1; }, ## -invalidcommand => sub {$_[0] = $_[2] }, )->pack(@pl);

    Ok, now a quick explanation on the above, the -validate     => 'key' line is simply the option that says I want to perform validation everytime the user presses a key in the widget, the line starting with -validatecommand is the (anonymous) subroutine that checks the data and should return true if the proposed contents of the widget are allowable, and false if they don't, in which case no change will be made to the actual content of the widget. First we shift in the first argument, which contains what the data will be if it passes validation, so in my case, I wanted to allow no more than one character, so I check that the length of $proposedtext is less than or equal to one. And since the return value of a subroutine is the same as the return value of the last statement executed, nothing further is needed to make the validation function as intended

    The line starting with -invalidcommand is where we keep the user from changing it to something we don't want, it also is probably a bit confusing looking. As I mentioned earlier, the first argument passed contains the value that the user is trying to change the entry widget to, I use $_[0] directly because I want to change the actual content of what it will be changed to, if you've used the -textvariable option, you can replace $_[0] with whatever variable you gave for that option, it will function just the same properly. And since what they're trying to type is invalid, we want to replace it with what the text was before they tried to change it, which is contained in the third argument passed to the subroutine ($_[2]), in this case I simply used $_[2] because I was too lazy to shift out the other values, it's not neccessarry to do that, and will likely make for better looking code.

    So, there you have it, a quick lesson in Tk::Entry Validation, and directions to where you can read a bit more on the subject.

    Update: removed extra parenthesis that had snuck in, and Commented out the -invalidcommand line, as I recently realized it is unneccessarry for this case, Tk::Entry already keeps you from adding anything that doesn't pass validation, also modifying $_[2] doesn't actually do anything, the only way to modify the contents of the Entry widget from within the invalidcommand sub is to modify the variable mentioned in the textvariable option, in which case it (according to the docs) resets the validation method to none. Also added better explanation for the -validatecommand option
    Just Another Perl Alchemist

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-19 23:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found