Ajout du multiprocessing pour que l'IHM ne freez pas

This commit is contained in:
Sdj Geek 2020-07-07 16:07:04 +02:00
parent 602ead50a9
commit b9beb304e6
1 changed files with 62 additions and 15 deletions

View File

@ -1,11 +1,17 @@
import tkinter as tk
import tkinter.ttk as ttk
import tkinter.filedialog as tkfiledialog
from multiprocessing import Process, Pipe
from trouver_decedes import trouver_decedes
class MainApplication(tk.Frame):
def __init__(self, parent, *args, **kwargs):
# Worker
self.run = False
self.pipe = None
# GUI
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
self.frame = tk.Frame(parent)
@ -54,24 +60,65 @@ class MainApplication(tk.Frame):
def command_button_dir_out(self):
self.value_dir_out.set(tkfiledialog.askdirectory(title="Répertoire de sortie"))
def tracker(self, step=None, text=None, set_max=None):
if step:
self.progressbar.step(step)
elif text:
print(text)
self.text_log.configure(state='normal')
self.text_log.insert(tk.END, text)
self.text_log.configure(state='disabled')
elif set_max:
self.progressbar['maximum'] = set_max
def add_log(self, text):
self.text_log.configure(state='normal')
self.text_log.insert(tk.END, text)
self.text_log.configure(state='disabled')
def watch(self):
if self.run:
if self.pipe.poll():
message = self.pipe.recv()
if message.get('step', False):
self.progressbar.step(message['step'])
elif message.get('text', False):
print(message['text'])
self.add_log(message['text'])
elif message.get('running', False):
self.run = message['running']
elif message.get('set_max', False):
self.progressbar['maximum'] = message['set_max']
self.parent.after(100, self.watch)
else:
print("Recherche terminée")
self.add_log("\nRecherche terminée\n")
self.button_valid.configure(state='normal')
def command_button_valid(self):
trouver_decedes(chemin_base_donnees=self.value_bdd_insee.get(),
chemin_repertoire_sortie=self.value_dir_out.get(),
numeros_unites=self.value_units.get().split(','),
tracker=self.tracker)
self.button_valid.configure(state='disabled')
self.run = True
(conn1, conn2) = Pipe()
self.pipe = conn1
Worker(conn2, self.value_bdd_insee.get(), self.value_dir_out.get(), self.value_units.get().split(',')).start()
self.watch()
class Worker(Process):
def __init__(self, pipe, bdd_insee, dir_out, units):
Process.__init__(self)
self.pipe = pipe
self.bdd_insee = bdd_insee
self.dir_out = dir_out
self.units = units
def tracker(self, step=None, text=None, set_max=None, running=None):
if step:
self.pipe.send({'step': step})
elif text:
self.pipe.send({'text': text})
elif set_max:
self.pipe.send({'set_max': set_max})
elif done:
self.pipe.send({'running': running})
def run(self):
trouver_decedes(chemin_base_donnees=self.bdd_insee,
chemin_repertoire_sortie=self.dir_out,
numeros_unites=self.units,
tracker=self.tracker)
self.pipe.send({'running': False})
if __name__ == "__main__":
root = tk.Tk()
root.title('Recherche des personnes décédées dans les registres')