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

Esteemed Monks,

Tk continues to amaze and confound me, today I have tried to create an interface where I can 'stroll' through records selected from a database table.

Here is a stripped down version of my package. As you can see the data to be displayed (and ultimately edited) is stored in a hash within the module. My problem is that if the _get_records_all method is called after the draw mthod has been called the data is not displayed.

What fundamental error have I made here? I hope someone can help!

package contact; use strict; use Tk; use Tk::BrowseEntry; use Data::Dumper; use InContact::cDBI; sub new { my ($self, %arg) = @_; bless { _nbpage => $arg{nbpage}, _record => $arg{record}, _iter => '', _iter_cnt => 0, _iter_index => 0, _ft => '', }, $self } sub draw { my ($self) = @_; my $fs = $self->{_nbpage}->Frame( -borderwidth => 2, -relief => 'groove', )->pack; $fs->Button( -text => 'All', -command => sub { $self->_get_records_all }, )->grid( $fs->Button( -text => 'Today', -command => sub { exit }, ), -sticky => 'w', -padx => 2, -pady => 2 ); $self->{_ft} = $self->{_nbpage}->Frame( -borderwidth => 2, -relief => 'groove', )->pack; $self->{_ft}->Label(-text => 'Contact ID' )->grid( $self->{_ft}->Entry( -textvariable => \$self->{_record}->{id +}, ), -sticky => 'w', -padx => 2, -pady => 2 ); $self->{_ft}->Label( -text => 'First Name' )->grid( $self->{_ft}->Entry( -textvariable => \$self->{_record}->{fi +rstname}, ), $self->{_ft}->Label( -text => 'Last Name' ), $self->{_ft}->Entry( -textvariable => \$self->{_record}->{la +stname}, ), $self->{_ft}->Label( -text => 'Company', ), $self->{_ft}->Entry( -textvariable => \$self->{_record}->{co +mpany}, ), -sticky => 'w', -padx => 2, -pady => 2 ); }
# _get_records_all # get all records and pass the first to the display sub _get_records_all { my $self = shift; $self->{_iter} = InContact::cDBI::contact->retrieve_all(); $self->{_iter_cnt} = $self->{_iter}->count(); $self->{_iter_index} = 0; my $obj = $self->{_iter}->first; $self->{_record} = $obj->hashy; print Dumper $self->{_record}; return; } 1;
updated: removed some irrelevant code.

Replies are listed 'Best First'.
Re: Perl/Tk - Can't see data?
by Courage (Parson) on Oct 04, 2004 at 19:41 UTC
    My guess - your Tk widgets are based on some references to scalar, which are no more used for database access.

    to be less vague, let us say, database read code does something like this:

    $obj->{_record} = {company=>'fkjfkdjfd',firstname=>'vmk'};
    or may be very differently, but similar in spirit; so there will be are another scalars.

    To say, to access those, you use same formulae $self->{_record}->{company} but actual scalar is another.

    You must understand that -textvariable=>\....... does not recalculates reference each time, it takes scalar reference which is lost by your further code.

    just a guess

    Best regards,
    Courage, the Cowardly Dog

      Ah haa!

      See, when I asked what fundamental error I made, little did I see that I had made a fundamental error. Thanks Courage. Each time I get a record I have to copy it into the 'static hash' that was referenced at draw, so I added a couple of lines here and there, and, voila, it works!

      Thanks once again!

      jdtoronto

Re: Perl/Tk - Can't see data?
by herveus (Prior) on Oct 04, 2004 at 18:41 UTC
    Howdy!

    I'm shooting from the hip here...

    Do you call update on your main window or the appropriate container? Is there a main loop running?

    yours,
    Michael
      herveus

      Yes, there is a main loop running.

      I tried updating the frame that has the Entry widgets in it after I changed the data (I can see the data chaanging on the console!) and I changed the code to pass in the MainWindow so I could update that as well, no joy.

      But thatnks for the tips, right now any hip is better than none!

      jdtoronto