Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Perl Tk text widget printing problem

by reaper9187 (Scribe)
on Nov 14, 2012 at 14:25 UTC ( #1003813=perlquestion: print w/replies, xml ) Need Help??
reaper9187 has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone,
I'm trying to develop this GUI that uses the perl Tk text widget to print certain values. However when i execute the print command for the text widget, i.e $txt->insert('end', "$worksheet->{Cells}[$row][$col]->{Val}"); the widget does not print the string. It only prints
TCH Drop: TCH Drop: TCH Drop:
I tried to check if the string was empty but it was not. I then output the string to the command window and got this result:
TCH Drop: A M J 0 0 1 B TCH Block: A M J 0 0 1 C TCH Drop: A M J 0 0 1 C TCH Block: B K T 0 0 1 A TCH Drop: B K T 0 0 1 A
I feel this has something to do with the structure of the string that is being passed although the actual string in the excel file looks something like BKT001A, AMJ001C, etc.. I'm new to perl and would really like some help with this problem... !!! Thanks ..!!!!!

Replies are listed 'Best First'.
Re: Perl Tk text widget printing problem
by zentara (Archbishop) on Nov 14, 2012 at 14:41 UTC
    I think your problem may be that Tk::Text's insert dosn't expect to have to dereference hashes. I would first try this:
    # $txt->insert('end', "$worksheet->{Cells}[$row][$col]->{Val}"); my $string = $worksheet->{Cells}[$row][$col]->{Val}; $txt->insert('end', $string);

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Perl Tk text widget printing problem
by thundergnat (Deacon) on Nov 14, 2012 at 15:41 UTC

    I agree with zentara; it looks like you are feeding insert a hash. See the following test code:

    use warnings; use strict; use Tk; my %w; my %hash = ( 'key1' => 'value1', 'key2' => 'value2' ); $w{mw} = MainWindow->new; $w{text} = $w{mw}->Text->pack; $w{text}->insert('end', %hash); $w{text}->insert('end', "\n\n" ); while ( my ($key, $value) = each %hash ){ $w{text}->insert('end', "$key => $value\n"); } MainLoop;
    Update: (after actual code snippet was posted) You are setting yourself up for trouble by accessing private internal data structures directly. Try using the accessor methods instead.

    $txt->insert('end', $worksheet->get_cell($row, $col)->value());
    Update 2:blarg. fixed broken hash iterator in demo code.
      thanks for helping .. works like a charm.. :) :)
Re: Perl Tk text widget printing problem
by reaper9187 (Scribe) on Nov 14, 2012 at 15:13 UTC
    i tried your version too .. but it's still the same .. any alternatives ???

      "the above code doesnt do me any good.."

      This is unconstructive. You don't go so far as to say what difference this made to your output. To avoid going around in circles with this problem (again) post a minmal example which recreates your problem, it doesn't have to be the entire program, just a short one which demonstrates the problem. Read and understand How do I post a question effectively?. Also you replied to yourself, not zentara.

      Update: Also, please mark updates you make to posts to avoid confusion.

        i apologize for the mistakes.. what i meant was that i tried substituting the code by zentara in my script but it didnt change anything . u can even try it with a basic parser script
        use Spreadsheet::ParseExcel; my $parser = Spreadsheet::ParseExcel->new(); my $workbook = $parser->parse($filename); if ( !defined $workbook ) { die $parser->error(), ".\n"; } for my $worksheet ( $workbook->worksheet(0) ) { my ( $row_min, $row_max ) = $worksheet->row_range(); my ( $col_min, $col_max ) = $worksheet->col_range(); for my $row ( $row_min .. $row_max ) { for my $col ( 1) { my $cell = $worksheet->get_cell( $row, $col ); next unless $cell; $text->insert('end', "$worksheet->{Cells}[$row][1]->{V +al}\n"); print "$worksheet->{Cells}[$row][1]->{Val}\n"; } } }
        NOte: i observed the following :
        1. The command seems to execute flawlessly on files where i reduced the no of rows (removed the entries) but it causes problems with unformatted files.
        2. The print command in such cases(unformatted files) prints the values as shwn in the first post while nothing is displayed in the text widget.
        Update: The above code is just a portion of the script, the portion i suspect is causing problems

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1003813]
Approved by herveus
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2018-12-15 14:12 GMT
Find Nodes?
    Voting Booth?
    How many stories does it take before you've heard them all?

    Results (69 votes). Check out past polls.