Esteemed Monks,

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'.

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');
Here is the sub that populates the page:
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; }
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 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"); }
and here is the Update sub:
# 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; }
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?

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?

jdtoronto


In reply to Updating values in Tk using a NoteBook container by jdtoronto

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.