mikasue has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellow monks!

Is there a way to redraw a frame in TK?

I've searched and haven't found a command that updates or redraws a widget.

The only options i've come up with is to destroy the frame and re-create it. I tried unpacking but when I repack it is the same content.

What I have is a main window then a new window pops up. When the new window is destroyed some information in the main window should update. Any commands that I can call to just update that frame or do I have to destroy and redraw it also?

Thanks!

Replies are listed 'Best First'.
Re: Redraw Frame in Perl/TK
by rcseege (Pilgrim) on Sep 20, 2006 at 20:57 UTC

    Update: Added example code

    Um... would you believe:

    $widget->update;

    Still, I can't help feeling that there might be something you're leaving out. Perhaps if you could provide a small working example that demonstrates your problem?

    The classic case that I've seen in Perl/Tk is that someone has a long running subroutine that does something and prevents the GUI from processing new events until the sub is complete. Take the following script:

    use strict; use Tk; my $counter = 0; my $mw = MainWindow->new(-title => "Counter"); $mw->Label(-textvariable => \&runCounter)->pack; $mw->Button( -text => 'Start Counter', -command => \&counter )->pack; MainLoop; sub runCounter { $counter = 0; while ($counter != 100000) { $counter++; } }

    In the preceding script, the window will seem to freeze once you press the "Start Counter" button, and the numbers will change to 100000 once the sub has completed. If those numbers were a progress bar, then that's not going to be acceptable. Also if you were to move a window in front of the Counter window while it's counting and then move it away, the screen will not be refreshed until the counter stops.

    Now, try modifying the runCounter sub to the following and rerun the script.

    sub runCounter { $counter = 0; while ($counter != 100000) { $counter++; $mw->idletasks; } }

    You should now see the Label updated as the counter increases, but note how idletasks does not update all new events. Try resizing the app while it counts, try moving a window in front of the app, then removing it. It should be more or less identical to before idletasks was added. Finally, change the sub to the following and rerun it.

    sub runCounter { $counter = 0; while ($counter != 100000) { $counter++; $mw->update; } }

    Now the app should be completely responsive to other events as it counts... a drawback of updating so often however is that it does add processing time to the sub to complete so it's fairly common to see code that only updates at certain intervals. Something like this perhaps:

    $mw->update if $counter % 100 = 0;

    Rob

Re: Redraw Frame in Perl/TK
by hgolden (Pilgrim) on Sep 20, 2006 at 20:55 UTC
    Hey

    If you're asking what I think you're asking, $mw->update; will do that, where $mw is whatever your Main Window is called.

    Hays

Re: Redraw Frame in Perl/TK
by GrandFather (Saint) on Sep 20, 2006 at 21:18 UTC

    Show us some code. The following updates the label in the main window before opening the dialog.

    use strict; use warnings; use Tk; use Tk::Dialog; my $mw = MainWindow->new; my $label = $mw->Label (-text => "Foo bar bang")->pack; my $button = $mw->Button (-text => "Bar bang foo", -command => \&popup +)->pack; MainLoop; sub popup { my $dialog = $mw->Dialog; $label->configure (-text => "Bar bang foo"); $dialog->Show (); }

    DWIM is Perl's answer to Gödel