Thread Zeitspanne überprüfen? (10 answers)
Opened by Me at 2013-09-23 21:33

hlubenow
 2013-09-24 18:25
#170577 #170577
User since
2009-02-22
875 Artikel
BenutzerIn
[default_avatar]
Hier nochmal das Beispiel (leicht verbessert) in Perl/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
64
65
66
67
#!/usr/bin/perl

use warnings;
use strict;

use Tk;

package myWindow;

    sub new {
        my $classname = shift;
        my $self = {t => 20,
                    mw => MainWindow->new(),
                    password => "Swordfish",
                    entered => "",
                    won => 0}; 
        $self->{mw}->optionAdd("*font", "Arial 15 normal");
        $self->{mw}->title("Time-Limit");
        $self->{mw}->geometry("+250+250");
        $self->{lab1} = $self->{mw}->Label(-text => $self->{t});
        $self->{lab1} -> pack(-padx => 20, -pady => 20);

        $self->{fr1} = $self->{mw}->Frame();
        $self->{lab2} = $self->{fr1}->Label(-text => "Enter Password:");
        $self->{lab2} -> pack(-anchor => 'w');
        $self->{entry1} = $self->{fr1}->Entry(-foreground => 'black',
                                              -background => 'white');
        $self->{entry1} -> pack();
        $self->{entry1} -> focus();
        $self->{entry1}->bind('<Return>', sub {$self->checkPass()});
        $self->{fr1} -> pack(-padx => 10, -pady => 10);
        $self->{mw}->after(1000, sub {$self->countdown()});

        return bless($self, $classname);
    }

    sub countdown {
        my $self = shift;
        if ($self->{won} != 0) {return;}
        $self->{t}--;
        if ($self->{t} > 0) {
            $self->{lab1}->configure(-text => $self->{t});
            $self->{mw}->after(1000, sub {$self->countdown()});
        } else {
            $self->{lab1}->configure(-text => "Verloren!");
            $self->{won} = -1;
        }
    }

    sub checkPass {
        my $self = shift;
        if ($self->{won} != 0) {return;}
        if ($self->{t} <= 0) {
            $self->{lab1}->configure(-text => "Too Late!");
            return;
        }
        $self->{entered} = $self->{entry1}->get();
        if ($self->{entered} eq $self->{password}) {
            $self->{entry1}->delete(0, 'end');
            $self->{lab1}->configure(-text => "Gewonnen!");
            $self->{won} = 1;
        }
    }
    
package main;
myWindow->new();
MainLoop();

View full thread Zeitspanne überprüfen?