Bonjour à tous,
Je débute en python.
J'ai un soucis pour arrêter un thread lorsque je clique sur la croix pour fermer mon appli.
Je reste bloqué dans le join().
A priori cela viendrait dans l'appel de la méthode getState() de ma class LabeledCheckbutton.
Hélas je ne comprends pas bien ce qui se passe.
D'avance, merci pour votre aide.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
#coding:utf-8
import tkinter as tk
import time
from threading import Thread
 
class LabeledCheckbutton(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.checkVar = tk.IntVar()
        self.checkbutton = tk.Checkbutton(self, text="Click on me.....", variable=self.checkVar)
        self.checkbutton.grid(row=1, column=0)
 
    def getState(self):
        return( self.checkVar.get() )
 
class MyFrame(tk.LabelFrame, Thread):
    def __init__(self, parent):
        tk.LabelFrame.__init__(self, parent)
        Thread.__init__(self)
        self.grid(row=0, column=0)
        self.terminateThread = False
        self.lblchk = LabeledCheckbutton(self)
        self.lblchk.grid(row=0, column=0)
        self.temp = 0
 
    def run(self):
        while self.terminateThread == False:
            time.sleep(2)
            self.temp = self.lblchk.getState()
            print("Checkbox value = " + str(self.temp))
        print("Exit Thread")
 
 
    def killTask(self):
        self.terminateThread = True
        print("killing Task !!!!!!!") 
 
 
def on_closing():
    myFrame.killTask()
    myFrame.join()
    print("Terminated !!!!!!!")
    root.destroy()
 
if __name__ == "__main__":
    root = tk.Tk()
    root.protocol("WM_DELETE_WINDOW", on_closing)
    myFrame = MyFrame(root)
    myFrame.start()
    root.mainloop()