1 2 3 4 5 6 7 8 9 10 11 12
eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm 10; my $answer = <STDIN>; alarm 0; }; if ($@) { say "Verloren"; } else { say "Gewonnen"; }
2013-09-23T20:14:10 GwenDragonHinweis: alarm läuft nicht auf Windows.
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
#!/usr/bin/env python import Tkinter class myWindow: def __init__(self): self.mw = Tkinter.Tk() self.mw.option_add("*font", ("Arial", 15, "normal")) self.mw.title("Time-Limit") self.mw.geometry("+250+250") self.t = 20 self.password = "Swordfish" self.entered = None self.lab1 = Tkinter.Label(self.mw, text = str(self.t)) self.lab1.pack(padx = 20, pady = 20) self.fr1 = Tkinter.Frame() self.lab2 = Tkinter.Label(self.fr1, text = "Enter Password:") self.lab2.pack(anchor = Tkinter.W) self.entry1 = Tkinter.Entry(self.fr1, bg = 'white') self.entry1.pack() self.entry1.focus() self.entry1.bind("<Return>", self.checkPass) self.fr1.pack(padx = 10, pady = 10) self.mw.after(1000, self.countdown) self.mw.mainloop() def countdown(self): if self.entered == self.password: return self.t -= 1 if self.t > 0: self.lab1.configure(text = str(self.t)) self.mw.after(1000, self.countdown) else: self.lab1.configure(text = "Verloren!") def checkPass(self, ev): if self.t <= 0: self.lab1.configure(text = "Too Late!") return self.entered = self.entry1.get() if self.entered == self.password: self.entry1.delete(0, Tkinter.END) self.lab1.configure(text = "Gewonnen!") if __name__ == "__main__": app = myWindow()
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();