in reply to Tk::ProgressBar and threads
If it was me I would start over, this dosn't show any progress, and of course the clear button won't work. So I hacked you code to make it work, but the way you have it set up, it's a 1 shot thread, because you detach it. Notice that if you hit Clear while the thread is running, it will be restored on the next update, because you have no shared control variable in the thread code block, to tell it to stop. See Tk-with-worker-threads for an example of how you can make reusable threads with Tk, without leaking memory on each run.
#!/usr/bin/perl use warnings; use strict; use threads; use threads::shared; use Tk; use Thread::Queue; use Tk::ProgressBar; my $percent_done; share $percent_done; $percent_done = 0; my $comm = new Thread::Queue; my $progressbar; threads->new(\&go, $comm, $percent_done)->detach; my $mw; ($mw, $percent_done) = create_gui(); $mw->repeat( 100, [\&updateScreen, $comm, $percent_done] ); MainLoop(); sub create_gui { $percent_done=0; my $mw = MainWindow->new( -title => 'Test'); $mw->Label( -text => "percent_done" )->pack; $progressbar = $mw->ProgressBar()->pack; $progressbar->value($percent_done); $mw->Button ( -text => "Clear", -command => sub { $percent_done = 0; $progressbar->value(0); $mw->update; } )->pack; return ($mw, $percent_done); } sub go { my ($comm, $percent_done) = @_; $comm->enqueue( $percent_done + 5); while(1) { $comm->enqueue( $percent_done + 5); select undef, undef, undef, 1; $percent_done += 5; if($percent_done == 100){last} } } sub updateScreen { my ($comm, $percent_d) = @_; $progressbar->value($percent_done); ($percent_done = $comm->dequeue ) if $comm->pending; $progressbar->value($percent_done); }
|
|---|