Well it seems like you just put together some code, and "hoped it would work". There are alot of problems in the way you set this up. First, it's "use threads" NOT "use Threads". Second, percent_done needs to be shared if you want to use it across threads in your &go sub. Third, you can't use -textvariable in a progressbar across threads, you need to start a timer, to periodically read $percent_done and update the progressbar. Fourth, you must start all thread code, before any Tk code is invoked.

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); }

I'm not really a human, but I play one on earth. flash japh

In reply to Re: Tk::ProgressBar and threads by zentara
in thread Tk::ProgressBar and threads by dannoura

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.