-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterface2.py
executable file
·285 lines (220 loc) · 7.62 KB
/
interface2.py
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env python3
#coding: utf8
""" Outil de recuperation du classement: interface graphique.
"
" :copyright: Copyright 2018, see AUTHORS
" Copyright 2018, voir AUTHORS
" :licence: CeCILL-C or LGPL, see COPYING for details.
" CeCILL-C ou LGPL, voir COPYING pour plus de details.
"
" Version très très très alpha.
"""
import sys
if sys.version_info[0] != 3:
print("Erreur -- Fonctionne avec Python 3.x")
exit(1)
import threading
import tkinter as tk
import queue
import signal
import platform
import palmares
# import palmares_dummy as palmares
from util import check_dependencies, exit_pause
# Global variables, uggly but avoids a wrapper or a functor
# or a higher level function or whatever
login_entr = None
mdp_entr = None
lic_entr = None
prof_entr = None
worker = None
root = None
# What happens when the button is clicked.
# (Main stuff is here)
def start( ):
global login_entr
global mdp_entr
global lic_entr
global prof_entr
global root
global worker
# On récupère les paramètres de l'utilisateur
login = login_entr.get()
mdp = mdp_entr.get()
lic = lic_entr.get()
prof_s = prof_entr.get()
# Check the depth is an integer (or can be converted to an int)
try:
prof = int( prof_s )
except ValueError:
print( "la profondeur n'est pas un entier" )
popProfondeur()
return
# Le calcul va afficher des choses sur la sortie standard.
# Pour que ça soit plus convivial, afficher ça dans une fenêtre.
root = tk.Tk()
root.wm_title( "Calcul du classement en cours" )
output = ThreadSafeText( root )
output.pack()
sys.stdout = Std_redirector( output )
# Bouton ok pour la fin du calcul, bouton stop pour tuer avant
stop = tk.Button( root, text = "Stop", command = stopThread )
stop.pack()
"""
ok = tk.Button( root, text = "Ok", command = endThread )
ok.pack()
"""
# Et enfin on appelle la fonction qui fait le calcul
worker = threading.Thread( target = wrapperFunction, args = ( login, mdp, lic, prof ) )
worker.daemon = True
worker.start()
# Affichage
root.mainloop()
worker.join()
root.destroy()
return
# J'ai fait ce wrapper au cas où on voudrait mettre des trucs
# genre de la gestion d'exceptions avec un gros try/catch autour de l'appel
def wrapperFunction( login, mdp, lic, prof ):
palmares.recupClassement( login, mdp, lic, prof )
return
# J'ai fait ces deux cas pour avoir typiquement
# - un bouton stop qui arrête le thread en background
# - un bouton ok qui ferme la fenêtre une fois que c'est terminé
def endThread():
global root
global worker
worker.join()
root.destroy
del sys.stdout
sys.stdout = sys.__stdout__
return
def stopThread():
import signal
import os
## Ceci ne fait rien du tout
## worker._Thread__stop()
## Donc méthode du bœuf.
#os.kill( os.getpid(), signal.SIGKILL )
os.kill( os.getpid(), signal.SIGUSR1 )
worker.join()
root.quit()
return
# Used to communicate with authority b/w the threads
def sighandler( signum, frame ):
print( "Caught signal" )
# sys.exit()
import _thread
_thread.exit()
#raise ExitThreadNow
class ExitThreadNow( Exception ):
def __init__( self, login="", pswd="", licence="", d="" ):
print( "Called exit thread" )
self.login = login
self.pswd = pswd
self.licence = licence
self.d = d
# Input problem
def popProfondeur( ):
wa = tk.Toplevel()
wa.wm_title( "Classement : warning" )
wt = tk.Label( wa, text = "La profondeur doit être un entier, généralement entre 2 et 4 ou 5" )
wt.config( font = ( "Verdana", 16), fg = "red" )
wt.pack()
ok = tk.Button( wa, text = "Ok", command = wa.destroy )
ok.pack()
return
# used to redirect an output (eg std[out,err]) on a widget
# (from stackoverflow)
class Std_redirector():
def __init__(self, widget):
self.widget = widget
def write(self,string):
self.widget.write(string)
# used to redirect an output (eg std[out,err]) on a widget
# (from stackoverflow)
class ThreadSafeText( tk.Text ):
def __init__(self, master, **options):
tk.Text.__init__(self, master, **options)
self.queue = queue.Queue()
self.update_me()
def write(self, line):
self.queue.put(line)
def update_me(self):
while not self.queue.empty():
line = self.queue.get_nowait()
self.insert(tk.END, line)
self.see(tk.END)
self.update_idletasks()
self.after(10, self.update_me)
def displayWarning():
warn = tk.Tk()
warn.wm_title( "Classement futur de tennis" )
txt1 = tk.Label( warn, text = "Attention" )
txt1.config( font=("Verdana", 20), fg = "red")
txt2 = tk.Label( warn, text = "Verion très très préliminaire, peu testée" )
txt2.config( font=("Verdana", 18), fg = "blue")
txt3 = tk.Label( warn, text = "En particulier, au moment de quitter (en particulier en cours de simulation), le programme risque d'avoir besoin d'un peu d'autorité", wraplength = 400 )
txt3.config( font=("Verdana", 18), fg = "blue")
ok = tk.Button( warn, text = "Ok, j'ai compris", command = warn.destroy )
ok.config( font=("Verdana", 20), fg = "green" )
ok.pack()
txt1.pack()
txt2.pack()
txt3.pack()
ok.pack()
return
# View
def main():
global login_entr
global mdp_entr
global lic_entr
global prof_entr
check_dependencies()
displayWarning()
signal.signal( signal.SIGUSR1, sighandler )
window = tk.Tk()
window.wm_title( "Classement futur de tennis" )
txt = tk.Label( window, text = "Bienvenue dans l'application classement" )
txt.config( font=("Verdana", 18), fg = "blue")
# Entry fields
login_txt = tk.StringVar()
login_entr = tk.Entry( window, textvariable = login_txt, width=18 )
login_label = tk.Label( window, text = "Votre login" )
login_label.config( font=("Verdana", 14))
mdp_txt = tk.StringVar()
mdp_entr = tk.Entry( window, show = "*", textvariable = mdp_txt, width=18 )
mdp_label = tk.Label( window, text = "Votre mot de passe" )
mdp_label.config( font=("Verdana", 14))
lic_txt = tk.StringVar()
lic_entr = tk.Entry( window, textvariable = lic_txt, width=18 )
lic_label = tk.Label( window, text = "Votre numéro de licence" )
lic_label.config( font=("Verdana", 14))
prof_txt = tk.StringVar()
prof_entr = tk.Entry( window, textvariable = prof_txt, width=18 )
prof_label = tk.Label( window, text = "Profondeur du calcul" )
prof_label.config( font=("Verdana", 14))
# Nice layout
txt.grid( row = 0, column = 0, columnspan = 3 )
login_label.grid( row = 1, column = 0)
login_entr.grid( row = 1, column = 1 )
mdp_label.grid( row = 2, column = 0)
mdp_entr.grid( row = 2, column = 1 )
lic_label.grid( row = 3, column = 0)
lic_entr.grid( row = 3, column = 1 )
prof_label.grid( row = 4, column = 0)
prof_entr.grid( row = 4, column = 1 )
# Buttons (Enter and quit)
go = tk.Button( window, text = "Lancer le calcul", command = start )
out = tk.Button( window, text = "Quitter", command = window.quit )
go.config( font=("Verdana", 16), fg = "green" )
out.config( font=("Verdana", 16), fg = "red" )
go.grid( row = 6, column = 1 )
out.grid( row = 6, column = 2 )
window.mainloop()
if __name__ == "__main__" :
try:
main()
except KeyboardInterrupt:
print("Interruption par l'utilisateur")
exit_pause()