Search code examples
perlperltk

Updating Tk label using Thread


use Thread;
use warnings;
use Tk;

my $x = 10;
my $mw = new MainWindow;
$mw->Label(-text => 'honeywell')->place(-x => $x, -y => 50);
my $thr = new Thread \&sub1;

sub sub1 { 
  for ($i = 0 ; $i < 20 ; $i++) {
      $x += 20;
      sleep(2);            
      $mw->update;
    }
}

MainLoop;                        

I am trying to update the label so that the text appears going down.I want to implement it using thread.But the text os not sliding down.Can anyone plz help me?


Solution

  • Try this code:

    use strict;
    use warnings;
    
    use Tk;
    
    my $x = 10;
    
    my $mw = new MainWindow;
    my $label = $mw->Label(-text => 'honeywell')->place(-x => $x, -y => 50);
    
    $mw->repeat(2000, \&sub1);
    
    sub sub1 {
        return if $x >= 400;
        $x += 20;
        $label->place(-x => $x, -y => 50);
        $mw->update;
    }
    
    MainLoop;