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

Re: Tk & threads & Excel OLE??

by Sandy (Curate)
on Jun 10, 2008 at 22:11 UTC ( [id://691350]=note: print w/replies, xml ) Need Help??


in reply to Tk & threads & Excel OLE??

I was fooling around to try to find a workable solution.

Because I was trying a variety of things, the solution below is rather untidy (stuff left over from things that didn't work)

However, I am running short of time, so I simply give you something that might be what you are looking for.

Main points:
1. Use $mainWindow->update() within loops. It keeps Tk alive
2. Launching process using after allows you to continue processing within your current process (kinda like thread?)
3. If you are using a system command that blocks your process, you should look at fileevent and pipes.

Good luck.

use strict; use warnings; use Tk; my $excelflag; my $mainWindow = MainWindow->new; my $textBox = $mainWindow->Scrolled("Text", -width=>80, -height=>7, -scrollbars=>"ose", -wrap=>"word")->pack(); #my $goBut = $mainWindow->Button(-foreground=>"blue", -text=>"Click", # -command=>sub { $thr = threads->new(\&excecute) } +)->pack(); my $goBut = $mainWindow->Button(-foreground=>"blue", -text=>"Click", -command=>sub { excecute() } )->pack(); my $stopBut = $mainWindow->Button(-foreground=>"red", -text=>"Stop", -command=>sub{ $excelflag = 1 })->pack(); my $exitBut = $mainWindow->Button(-foreground=>"red", -text=>"Exit", -command=>sub{ $excelflag = 1; exit })->pack(); MainLoop; sub excecute { $goBut->configure(-state=>"disabled"); $textBox->configure(-state=>"normal"); $excelflag = 0; $textBox->after(2,\&excel); open(FILE,"C:/file.txt") or die "Can't open file.\n"; while(<FILE>) { my $message = $_; $textBox->insert('end', $message); } close(FILE); $textBox->configure(-state=>"disabled"); $textBox->see('end'); return; } sub excel { use Win32::OLE; use Win32::OLE::Const 'Microsoft Excel'; my ($Excel,$Workbook,$fileOutput,$CurrentSheet,$Range); $fileOutput = "C:/test.xls"; $Excel = Win32::OLE->new('Excel.Application', 'Quit') || die "Can't create Excel object. \n"; $Excel->{'Visible'} = 1; #0 is hidden, 1 i +s visible $Excel->{DisplayAlerts} = 0; #0 is hide alerts + $Excel->{SheetsInNewWorkbook} = 1; $Workbook = $Excel->Workbooks->Add(); $Workbook->SaveAs($fileOutput) or die "Can't save Excel.\n"; $CurrentSheet = $Workbook->Worksheets(1); $CurrentSheet->Select; for (my $i=1; $i<=6500; $i++) { $Range = $CurrentSheet->Range("A$i"); $Range->{Value} = $i; sleep(0.01); $mainWindow->update(); last if $excelflag; } $Workbook->Save(); $Workbook->Close(); $Excel->Quit(); Win32::OLE->FreeUnusedLibraries(); $goBut->configure(-state=>"normal"); return; }
Disclaimer: this could be a lot cleaner - but want to go home.

Sandy

Replies are listed 'Best First'.
Re^2: Tk & threads & Excel OLE??
by Slickuser (Initiate) on Jun 10, 2008 at 23:01 UTC
    Thanks for the code Sandy. When I test the code you gave to me, I get my GUI to freeze up until it's done running the Excel part. It's the same way I have before, and this is why I switch to threads to prevent the GUI from freezing up.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://691350]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-25 16:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found