jdtoronto has asked for the wisdom of the Perl Monks concerning the following question:
I have been asked to re-do the UI on one of my applications using Perl/Tk.
I have a Tk::Notebook with a BrowseEntry and some entry widgets in it. With the same widgets in a TopLevel if I change the values being bound to the widgets then the widget display changes. But inside the NoteBook this does not seem to happen!
Here is the code to set-up the NoteBook and to call a sub to populate 'page1'.
Here is the sub that populates the page:my $mw = MainWindow->new(); $mw->geometry("600x400+30+30"); my $nb = $mw->NoteBook()->pack(-expand => 1, -fill => 'both', -pad +x => '20', -pady => '30'); # Page 1 my $p1 = $nb->add('page1', -label => 'Users'); &doUserPageUi($p1, $dbh); # Page 2 my $p2 = $nb->add('page2', -label => 'Sources'); # Page 3 my $p3 = $nb->add('page3', -label => 'Projects'); # Page 4 my $p4 = $nb->add('page4', -label => 'Import'); # Page 5 my $p5 = $nb->add('page5', -label => 'Pull List');
Now when I select a user form the drop-down list I can get the LabEntry's to populate correctly. But as soon as I update the record (see sub doUserUpdate) - NO LUCK! Here is the search sub:sub doUserPageUi { my ($pg, $dbh) = @_; my @listnames; $pg->Label(-text => 'Username', -justify => 'right', -width => '20 +') ->grid(my $user_list = $p1->BrowseEntry(-variable => \$sta +te->{srcUname}, -browsecmd => \&doUserSearch, -width => '20'), -stick +y => 'w', -pady => '20'); $pg->Label(-text => 'First Name', -justify => 'right', -width => ' +20') ->grid($p1->LabEntry(-textvariable => \$user->{firstname}, + -width => '20'), -sticky => 'w', -pady => '5'); $pg->Label(-text => 'Last Name', -justify => 'right', -width => '2 +0') ->grid($p1->LabEntry(-textvariable => \$user->{lastname}, +-width => '20'), -sticky => 'w', -pady => '5'); $pg->Label(-text => 'Password', -justify => 'right', -width => '20 +') ->grid($p1->LabEntry(-textvariable => \$user->{password}, +-width => '20'), -sticky => 'w', -pady => '5'); $pg->Button(-text => 'Save/Update', -command => \&doUserUpdate)->g +rid(-columnspan => '2', -pady => '30'); my $SQL = "SELECT username FROM user;"; debug("SQL = $SQL"); my $sth = $dbh->prepare( $SQL ) or die $DBI::errstr; my $exec = $sth->execute; debug("Prepare result: $exec"); while ( my ($username) = $sth->fetchrow_array() ) { push @listnames, $username; debug("Username: $username"); } $sth->finish; debug("@listnames"); $user_list->insert('end', @listnames); return; }
and here is the Update sub:sub doUserSearch { debug("+doUserSearch"); my $uname = $state->{srcUname}; debug("$uname"); my $SQL = "SELECT username, firstname, lastname, password FROM us +er WHERE username =\'$uname\'"; my $sth = $dbh->prepare( $SQL ); $sth->execute; my $in = $sth->fetchrow_hashref; if ( $in->{username} ne $uname ) { # user does not exist so clear values and set up a new usernam +e value. $user->{firstname} = ""; $user->{lastname} = ""; $user->{password} = ""; $user->{username} = $uname; } else { $user->{firstname} = $in->{firstname}; $user->{lastname} = $in->{lastname}; $user->{password} = $in->{password}; $user->{username} = $in->{username}; } debug("$user->{username} $user->{firstname} $user->{lastname} $use +r->{password}"); $nb->raise('page1'); debug("-doUserSearch"); }
Just to recap - I can search as many times as I like. But as soon as I do an update that is it, I can never see the data in the LabEntry's again. Any clues please learned ones?# Save updated information to database sub doUserUpdate { if ($state->{srcUname}) { my $sth = $dbh->prepare( "REPLACE INTO user (username, firstna +me, lastname, password) VALUES (?, ?, ?, ?);"); $sth->execute( $user->{username}, $user->{firstname}, $user->{ +lastname}, $user->{password} ); $user = (); $state->{srcUname} = (); $nb->raise('page1'); } return; }
I just had a thought!!! Which does happen ocassionally. Am I destroying the relationship between the $user hashref being used in the display part of the cart by doing$user=(); on it in the update sub?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Updating values in Tk using a NoteBook container
by jdtoronto (Prior) on Aug 26, 2003 at 02:06 UTC |