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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division
import Tkinter
import threading
import time
###############################################################################
def sec2hms(sd):
"""sec2hms(sd): transforme les secondes sd en chaine "hh:mm:ss" pour affichage"""
h=0
m=0
s=sd
if s >= 60:
m = s//60
s -= m*60
if m >= 60:
h = m//60
m -= h*60
return "%02d:%02d:%02d" % (h, m, s)
##################################################################
class Comptearebours(threading.Thread):
def __init__(self, h, m, s):
threading.Thread.__init__(self)
self.t = h*3600 + m*60 + s
self.encore = True
def run(self):
global app
t1 = int(time.time())
app.varsaisie.set(sec2hms(self.t))
while self.encore:
t2 = int(time.time())
if t2>t1:
self.t -= t2-t1
if self.t <= 0:
self.t = 0
self.encore = False
app.varsaisie.set(sec2hms(self.t))
t1 = t2
time.sleep(0.01)
def stop(self):
self.encore = False
##################################################################
class Application(Tkinter.Frame):
def __init__(self, master=None):
Tkinter.Frame.__init__(self, master)
self.grid()
self.varsaisie = Tkinter.StringVar()
self.varsaisie.set("")
self.saisie=Tkinter.Entry(self, background="white", width=50, textvariable=self.varsaisie)
self.saisie.grid(row=0,column=0,padx=3, pady=6)
self.depart = Tkinter.Button(self, text=" Départ ", command=lambda:self.partir(self))
self.depart.grid(row=1, column=0, padx=3, pady=3, sticky="e")
self.stop = Tkinter.Button(self, text=" stop ", command=lambda:self.stopper(self))
self.stop.grid(row=1, column=1, padx=3, pady=3, sticky="w")
self.saisie.focus_set()
def partir(self,event):
self.chrono = Comptearebours(0,0,20)
self.chrono.setDaemon(True)
self.chrono.start()
def stopper(self,event):
self.chrono.stop()
##################################################################
def centrefenetre(fen):
"""place la fenêtre au milieu de l'écran"""
def geoliste(g):
r=[i for i in range(0,len(g)) if not g[i].isdigit()]
return [int(g[0:r[0]]),int(g[r[0]+1:r[1]]),int(g[r[1]+1:r[2]]),int(g[r[2]+1:])]
fen.update_idletasks()
l,h,x,y=geoliste(fen.geometry())
fen.geometry("%dx%d%+d%+d" % (l,h,(fen.winfo_screenwidth()-l)//2,(fen.winfo_screenheight()-h)//2))
##############################################################################
# lancement et affichage au centre de l'écran de l'application
#
if __name__ == "__main__":
fen=Tkinter.Tk()
fen.title(u"Compte à rebours")
app=Application(fen)
centrefenetre(fen)
fen.mainloop() |