Thread Verzögerung von Ereignissen (17 answers)
Opened by piet at 2015-06-02 16:18

hlubenow
 2015-06-02 19:19
#181238 #181238
User since
2009-02-22
875 Artikel
BenutzerIn
[default_avatar]
Beispiel in Tk:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/perl

use warnings;
use strict;

use Tk;

package Window;

    sub new {
        my $classname = shift;
        my $self = {timerstart => 3};
        return bless($self, $classname);
    }

   sub showWindow {
        my $self = shift;
        $self->{timer} = $self->{timerstart};
        $self->{mw} = MainWindow->new();
        $self->{mw}->optionAdd("*font", "Arial 12 normal");
        $self->{mw}->title("Timer");
        $self->{mw}->geometry("+416+347");
        $self->{mw}->bind('<Control-q>', sub { $self->{mw}->destroy() });
        $self->{lab} = $self->{mw}->Label(-text => $self->{timer});
        $self->{lab}->pack(-padx => 20, -pady => 20);
        $self->{fr1} = $self->{mw}->Frame();
        $self->{btn_start} = $self->{fr1}->Button(-text => "Start",
                                                  -command => sub { $self->start() });
        $self->{btn_start}->pack(-side => "left", -padx => 50);
        $self->{fr1}->pack();
        $self->{mw}->MainLoop();
    }

    sub start {
        my $self = shift;
        if ($self->{timer} < $self->{timerstart}) {
            return;
        }
        $self->{mw}->after(100, sub { $self->doTimer() });
    }

    sub doTimer {
        my $self = shift;
        $self->{timer} -= 0.1;
        $self->{timer} = sprintf("%.1f", $self->{timer});
        $self->updateLabel($self->{timer});
        if ($self->{timer} > 0) {
            $self->{mw}->after(100, sub { $self->doTimer() });
        } else {
            $self->updateLabel("Timer abgelaufen");
            $self->{timer} = $self->{timerstart};
        }
    }

    sub updateLabel {
        my $self = shift;
        my $text = shift;
        $self->{lab}->configure(-text => $text);
    }

package main;
my $w = Window->new();
$w->showWindow();

(Das Objektorientierte daran hatte ich gerade hier erklärt.)

View full thread Verzögerung von Ereignissen