-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreloj3.py
More file actions
executable file
·162 lines (148 loc) · 6.38 KB
/
Copy pathreloj3.py
File metadata and controls
executable file
·162 lines (148 loc) · 6.38 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Una copia del programa que hice en python 2 para tomar el tiempo en
# una partida de ajedrez pero este es para python 3.
from tkinter import *
from tkinter import font
import datetime
class RelojAjedrez(Frame):
def __init__(self, master = None):
Frame.__init__(self, master)
self.grid(sticky = N + S + E + W)
self.crearControles()
def crearControles(self):
top = self.winfo_toplevel()
top.rowconfigure(0, weight = 1)
top.columnconfigure(0, weight = 1)
self.rowconfigure(2, weight = 1)
self.columnconfigure(0, weight = 1)
self.columnconfigure(1, weight = 1)
letrasDisp = font.Font(family = "Courier",
size = 20, weight = "bold")
self.lbTiNegras = Label(self, text = "00:00:00",
font = letrasDisp, bg = "#000",
fg = "#FFF")
self.lbTiNegras.grid(row = 0, column = 0)
self.lbTiBlancas = Label(self, text = "00:00:00",
font = letrasDisp, bg = "#FFF",
fg = "#000")
self.lbTiBlancas.grid(row = 0, column = 1)
self.bGNegras = Button(self, text = "Negras",
command = self.bGNegrasClick)
self.bGNegras.grid(row = 1, column = 0)
self.bGBlancas = Button(self, text = "Blancas",
command = self.bGBlancasClick)
self.bGBlancas.grid(row = 1, column = 1)
self.bind_all("<space>", self.teclaPres)
self.bind_all("<Shift_L>", self.teclaPres)
self.bind_all("<Shift_R>", self.teclaPres)
self.bGBlancas.grid()
self.iniciarCont()
self.mostrarReloj()
def iniciarCont(self):
# cantidad de minutos
cantmin = 5
# la variable para llevar el reloj Negro
self.rNegras = datetime.timedelta(minutes = cantmin)
# la variable para llevar el reloj Blanco
self.rBlancas = datetime.timedelta(minutes = cantmin)
# variable para guardar cuanto tiempo había en un reloj al
# comienzo de la cuenta
self.dComienzo = datetime.timedelta(minutes = cantmin)
# variable para tomar tiempo en cada tick y compararlo con el
# que se toma al inicio del intervalo en referencia
self.tMuestra = datetime.datetime.min
# Tiempo que se toma al principio del intervalo para referencia
self.tReferencia = datetime.datetime.min
# si es true el reloj referenciado es el blanco, sino el negro
self.refBlancas = False
# una variable entera para guardar el id que devuelve el after
# en caso de que se necesite cancelarlo
self.idTempo = None
# indica que el reloj está iniciado y contando
self.contando = False
def relojTick(self):
self.tMuestra = datetime.datetime.now()
# si el reloj blanco está seleccionado
if (self.refBlancas):
# que descuente tiempo del reloj blanco
self.rBlancas = self.dComienzo - (
self.tMuestra - self.tReferencia)
if self.rBlancas <= datetime.timedelta(0):
self.rBlancas = datetime.timedelta(0)
self.idTempo = None
self.contando = False
else:
self.idTempo = self.after(100, self.relojTick)
else:
# si no que descuente del reloj negro
self.rNegras = self.dComienzo - (
self.tMuestra - self.tReferencia)
if self.rNegras <= datetime.timedelta(0):
self.rNegras = datetime.timedelta(0)
self.idTempo = None
self.contando = False
else:
self.idTempo = self.after(100, self.relojTick)
self.mostrarReloj()
def mostrarReloj(self):
self.lbTiNegras.config(text = "%02d:%02d:%02d" % (
self.rNegras.seconds / 60,
self.rNegras.seconds % 60,
self.rNegras.microseconds / 10000))
self.lbTiBlancas.config(text = "%02d:%02d:%02d" % (
self.rBlancas.seconds / 60,
self.rBlancas.seconds % 60,
self.rBlancas.microseconds / 10000))
def detenerBlancas(self, detenerRBlanco):
# si alguno de los relojes no tiene tiempo
if ((self.rBlancas <= datetime.timedelta(0))
or (self.rBlancas <= datetime.timedelta(0))):
self.iniciarCont()
# si me piden detener el reloj blanco
if detenerRBlanco and (not self.contando
or (self.contando and self.refBlancas)):
# si hay un reloj activado que lo detenga
if self.idTempo != None:
self.after_cancel(self.idTempo)
# que detenga el reloj blanco
self.rBlancas = self.dComienzo - (
self.tMuestra - self.tReferencia)
# y que inicie el reloj negro
self.dComienzo = self.rNegras
self.tMuestra = self.tReferencia = datetime.datetime.now()
self.refBlancas = False
# si me piden detener el reloj negro
elif (not detenerRBlanco
and (not self.contando
or (self.contando and not self.refBlancas))):
# si hay un reloj activado que lo detenga
if self.idTempo != None:
self.after_cancel(self.idTempo)
# que detenga el reloj negro
self.rNegro = self.dComienzo - (
self.tMuestra - self.tReferencia)
# y que inicie el reloj blanco
self.dComienzo = self.rBlancas
self.tMuestra = self.tReferencia = datetime.datetime.now()
self.refBlancas = True
else:
return
# ahora estamos contando
self.contando = True
# hay que poner a correr el reloj
self.idTempo = self.after(100, self.relojTick)
def bGNegrasClick(self):
self.detenerBlancas(False)
def bGBlancasClick(self):
self.detenerBlancas(True)
def teclaPres(self, event):
if (event.keysym == "space"):
self.detenerBlancas(self.refBlancas)
elif (event.keysym == "Shift_L"):
self.detenerBlancas(False)
elif (event.keysym == "Shift_R"):
self.detenerBlancas(True)
app = RelojAjedrez()
app.master.title("Reloj de Ajedrez")
app.mainloop()