Thread continus de tourner une fois la fenêtre tkinter fermée
Bonjour,
J'ai un code qui vient piloter une machine via requête http, dans ce code 2 threads sont lancés pour mettre à jour la fenêtre ou l'ip
Code:
1 2 3 4 5
| def launch_etat_maj_IP():
dict_thread['thread_ip'] = threading.Thread(target=maj_IP)
dict_thread['thread_ip'].start()
dict_thread['thread_state'] = threading.Thread(target=etat)
dict_thread['thread_state'] .start() |
Voici les fonctions qu'ils exécutent:
Code:
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
| def etat():
tmps.sleep(0.5)
while not dict_thread['End'].is_set():
try:
dict_global['r_ip'] = requests.get(f'{(maj_IP()[1])}LinkState', timeout=2)
dict_global['r'] = requests.get(f'{(maj_IP()[1])}TransportCommand', timeout=2)
dict_global['r_value'] = dict_global['r'].json().get('value')
tmps.sleep(0.2)
root.after(0,lambda:dict_ig['l_ip'].configure(image=dict_ig['photo_VOYANT_V']))
dict_global['AJA_linked'] = True
except requests.exceptions.RequestException:
root.after(0,lambda:dict_ig['l_ip'].configure(image=dict_ig['photo_VOYANT_R']))
dict_global['AJA_linked'] = False
if dict_global['r_value'] == '3' and dict_global['heure_SET']== False and dict_global['AJA_linked'] == True :
root.after(0,lambda:dict_ig['b_record'].config(image=dict_ig['photo_STOP'], text='Stop', command=lambda: stop()))
elif dict_global['r_value'] == '4' and dict_global['heure_SET']== False and dict_global['AJA_linked'] == True:
root.after(0,lambda:dict_ig['b_record'].config(image=dict_ig['photo_REC'], text='Record', command=lambda: record()))
elif dict_global['r_value'] == '3' and dict_global['heure_SET']== True and dict_global['AJA_linked'] == True:
root.after(0,lambda:dict_ig['b_record'].config(image=dict_ig['photo_REC_GRIS'], text='Stop\n'f"{dict_global['heure']}:{dict_global['minutes']}", command=lambda: stop()))
elif dict_global['r_value'] == '4' and dict_global['heure_SET']== True and dict_global['AJA_linked'] == True:
root.after(0,lambda:dict_ig['b_record'].config(image=dict_ig['photo_STOP_GRIS'], text='Record\n'f"{dict_global['heure']}:{dict_global['minutes']}", command=lambda: record()))
elif dict_global['r_value'] == '3' and dict_global['heure_SET']== False and dict_global['AJA_linked'] == False :
root.after(0,lambda:dict_ig['b_record'].config(image=dict_ig['photo_REC'], text='Record', command=lambda: record()))
elif dict_global['r_value'] == '4' and dict_global['heure_SET']== False and dict_global['AJA_linked'] == False :
root.after(0,lambda:dict_ig['b_record'].config(image=dict_ig['photo_REC'], text='Record', command=lambda: record()))
else:
pass |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| def maj_IP():
while not dict_thread['End'].is_set():
def get_ip():
dict_global['ip']= f"{dict_ig['entry_IP_A'].get()}.{dict_ig['entry_IP_B'].get()}.{dict_ig['entry_IP_C'].get()}.{dict_ig['entry_IP_D'].get()}"
return dict_global['ip']
root.after(0,lambda:get_ip)
SET = f"http://{dict_global['ip']}/config?action=set¶mid=eParamID_"
GET = f"http://{dict_global['ip']}/config?action=get¶mid=eParamID_"
return[SET,GET]
else:
pass |
Mon problème étant que lorsque je ferme ma fenêtre mon terminal ne revient pas automatiquement à la ligne, comme ci mon code tournait toujours. J'ai essayé plusieurs méthodes suite à des recherches qui m'indiquait que les interactions avec les widgets tkinter devait se passer dans le thread principal et non dans un thread secondaire, j'ai essayé l'élément join() mais mon code restait figé et je devais le fermer à l'aide du gestionnaire de tâche.
Quelqu'un aurait-il une idée ?
Merci d'avance