Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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

Comment on Perl Tk text widget printing problem
Select or Download Code
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 reaper9187 (Scribe) on Nov 14, 2012 at 15:13 UTC
    ^zentara...
    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.

        hi...
        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
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.
      Hi...
      thanks for helping .. works like a charm.. :) :)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2014-11-27 21:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (188 votes), past polls