£okì has asked for the wisdom of the Perl Monks concerning the following question:

I'm fairly new to Tk as I have now given up on Win32::GUI. My question about it is I have Entry fields which start blank. However, When I click a button, I want them to be filled with information based off another field.

I am to the point where I have all of the data ready but I can't figure out how to update the Entry fields. . Anyone know how?

below is my code, I am sorry about how long it is. I didn't think I'd get stuck this far in. I have deleted the menu subrouties to make this a little shorter for everyone. (they work don't worry about them.)

use strict; use Tk 800.00; use Mysql; require Tk::Frame; require Tk::TextUndo; require Tk::Text; require Tk::Scrollbar; require Tk::Menu; require Tk::Menubutton; require Tk::Adjuster; require Tk::DialogBox; # Main Window my $mw = new MainWindow; $mw->geometry('800x600'); # Frames Setup my $lf = $mw->Frame; # Left Frame; my $rf = $mw->Frame; # Right Frame; # Menu Bar Setup my $mb = $mw->Menu(-menuitems => &menubar_menuitems() ); $mw->configure(-menu => $mb); $mw->title("INSCO Inventory Control"); my($label_input) = $lf->Label( -text=>'Serial Number: ', ); my $tf_serial=$lf->Entry(-width=>30); my ($label_space) = $lf->Label( -text=>'', ); my $returned = ""; my $button_submit = $mw->Button(-text => 'Search', -command => $returned = sub{return(get_data($tf_serial))} ); my($label_po) = $rf->Label( -text=>'PO: ', ); my $tf_po=$rf->Entry(-width=>30); my($label_client) = $rf->Label( -text=>'Client: ', ); my $tf_client=$rf->Label(-width=>30, -text=>$returned); my($label_contact) = $rf->Label( -text=>'Contact: ', ); my $tf_contact=$rf->Entry(-width=>30); my($label_phone) = $rf->Label( -text=>'Contact Phone Number: ', ); my $tf_phone=$rf->Entry(-width=>30); my($label_email) = $rf->Label( -text=>'Contact Email Address: ', ); my $tf_email=$rf->Entry(-width=>30); my($label_address) = $rf->Label( -text=>'Contact Street Address: ', ); my $tf_address=$rf->Entry(-width=>'75'); my $tf_city=$rf->Entry(-width=>30); my $tf_state=$rf->Entry(-width=>30); my $tf_zipcode=$rf->Entry(-width=>30); my($label_id) = $rf->Label( -text=>'Client\'s Product ID: ', ); my $tf_id=$rf->Entry(-width=>30); my($label_model) = $rf->Label( -text=>'Product Model: ', ); my ($tf_model)=$rf->Entry(-width=>30); # Pack everything $lf->pack(qw/-side left -fill y/); $rf->pack(qw/-side right -fill y/); $label_input->pack(qw/-side top/); $tf_serial ->pack(qw/-side top/); $label_space ->pack(qw/-side top/); $button_submit ->pack(qw/-side top/); $label_po->pack(qw//); $tf_po->pack(qw//); $label_client->pack(qw//); $tf_client->pack(qw//); $label_contact->pack(qw//); $tf_contact->pack(qw//); $label_phone->pack(qw//); $tf_phone->pack(qw//); $label_email->pack(qw//); $tf_email->pack(qw//); $label_address->pack(qw//); $tf_address->pack(qw//); $tf_city->pack(qw//); $tf_state->pack(qw//); $tf_zipcode->pack(qw//); $label_id->pack(qw//); $tf_id->pack(qw//); $label_model->pack(qw//); $tf_model->pack(qw//); # Start the main event loop MainLoop; sub get_data { my ($widget) = @_; my $entered = $widget -> get(); my $dbh = Mysql->connect("localhost", "insco_inventory", "INSCO", +"hernagra") or die("Could Not Connect To Server"); my $query = qq~SELECT * FROM inventory WHERE serial='$entered'~; my $sth = $dbh->query($query); my @arr = $sth->fetchrow; return($arr[2]); }

Edit by tye, add READMORE

Replies are listed 'Best First'.
Re: Tk Field Changes
by graff (Chancellor) on Mar 20, 2003 at 04:45 UTC
    jasonk's advice should handle the question at hand. On a side note, you might consider structuring your Entry widgets and their associated text variables in a hash:
    my %field; my @names = (qw/serial phone email po address city state zip etc/); my @sizes = ( 30, 30, 30, 30, 75, 30, 30, 30, 80 ); my @labls = ("Serial Number:", "Phone Number:", "and so on..."); foreach (@names) { $field{$_}{txvar} = ''; $field{$_}{label} = $rf->Label(-text => shift( @labls )); $field{$_}{entry} = $rf->Entry(-width => shift( @sizes ), -textvariable => \${$field{$_}{txvar +}}, ); }
    This form of array initialization can probably be improved upon -- maybe you would prefer a hash of arrays, the hash keyed by field name and each array being the label string, DB field name, textvariable scalar, and widget handle. But any sort of hash array setup will be more compact and easier to maintain/adapt that a set of discrete scalars.

    (Also, note that you don't really need to keep track of the handles for those label widgets, unless you plan to recongifure them along with the Entry widgets as part of the GUI's behavior.)

      Thanks guys that helped a lot. I like the hash idea, glad you thought of it. Makes everything a lot neater.
Re: Tk Field Changes
by jasonk (Parson) on Mar 19, 2003 at 21:12 UTC

    If you give your entries a -textvariable option, then you can just set that variable and the text will change.


    We're not surrounded, we're in a target-rich environment!