bonjour ,

Je débute avec Tkinter , et je veux faire varier une data de température ( après acquisition ) .

Comment avec une boucle while renouveler , un champs , toutes les 30 secondes avec sleep ?

a / Dans la class Application(Tk):

b / Ou après __name__ == '__main__':

Mon code est le suivant :

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
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
 
from Tkinter import *
from tkSimpleDialog import Dialog
from time import sleep
import threading
import random
 
class AnimThread(threading.Thread):
 
    def __init__(self,canvas):
        threading.Thread.__init__(self)
 
        self.canvas=canvas
        self.item=self.canvas.create_text(300,350,fill="red",text="TEMPÉRATURE",font=("Times", "42", "bold italic") )
        self.item=self.canvas.create_text(300,100,fill="red",text="DEGRÉ : °C",font=("Times", "42", "bold italic") )
        self.item=self.canvas.create_text(300,500,fill="blue",text=int(25* random.random()),font=("Times", "62", "bold italic") )
 
        self.item=self.canvas.create_oval(280,200,330,250,fill="blue" )
 
    def run(self):
        self.anim=True
        state=0
        while(self.anim):
            state=[1,0][state]
            self.canvas.itemconfigure(self.item,state=[HIDDEN,NORMAL][state])
            self.canvas.update_idletasks()
            sleep(.2)
 
    def stop(self):
        self.anim=False
 
class AnimWidget(Canvas):
 
    def __init__(self,master):
        Canvas.__init__(self,master=master,bd=1,width=600,height=600)
 
        self.thread=AnimThread(self)
 
        self.start()
 
    def start(self):
        """start the thread"""
        self.thread.start()
 
    def stop(self):
        """stop the thread"""
        self.thread.stop()
        while(self.thread.isAlive()):
            pass
        self.thread=None
 
class MyDialog(Dialog):
 
    def __init__(self,master):
        Dialog.__init__(self,parent=master,title="titre" )
 
    def cancel(self, event=None):
        self.anim.stop()
        Dialog.cancel(self,event)
 
    def body(self, master):
        self.anim=AnimWidget(master)
        self.anim.pack()
 
class Application(Tk):
 
    def __init__(self):
        Tk.__init__(self)
 
        self.mainframe=self
 
        self.anim=AnimWidget(self)
        self.anim.pack()
 
        self.btn=Button(self,text="dlg",command=self.opendialog)
        self.btn.pack()
 
    def opendialog(self):
        dlg=MyDialog(self)
 
 
if __name__ == '__main__':
    app = Application()  
    app.mainloop()