Bonjour je n’arrive pas à accéder à une variable d’une classe.
Voici le code:
[code]#!/usr/bin/python
-- coding: utf-8 --
from Tkinter import *
from time import sleep
import threading
class Coucou(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.encore = True
def run(self):
while self.encore:
print("interruption")
Toto.a = 0
sleep(20)
def stop(self):
self.encore = False
class Toto(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.encore = True
def run(self):
self.a = 0
while self.a != 10 and self.encore:
chaine="a = " + str(round(self.a*10, 5))
if self.a < 6:
self.a = round(self.a + 0.01, 5)
app.label1.config(text=chaine)
sleep(0.1)
else:
self.a = round(self.a + 0.01, 5)
app.label1.config(text=chaine)
sleep(1)
def stop(self):
self.encore = False
class Pwm(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.creer_applets()
def creer_applets(self):
self.label1 = Label(fen, text="a = 0")
self.label1.pack(fill=X, side=TOP)
self.bouton1 = Button(fen, text="Lancer", command=lambda:self.deb_pwm())
self.bouton1.pack(fill=X, side=TOP)
self.bouton2 = Button(fen, text="Arrêt", command=lambda:self.fin_pwm())
self.bouton2.pack(fill=X, side=TOP)
self.bouton4 = Button(fen, text="Quitter", command=self.aurevoir)
self.bouton4.pack(fill=X, side=TOP)
def deb_pwm(self):
self.toto = Toto()
self.toto.setDaemon(True)
self.toto.start()
self.coucou = Coucou()
self.coucou.setDaemon(True)
self.coucou.start()
def fin_pwm(self):
self.toto.stop()
self.coucou.stop()
def aurevoir(self):
fen.destroy()
if name == ‘main’:
fen = Tk()
app = Pwm(fen)
fen.mainloop()
[/code]
La variable que je souhaite modifier est “a” qui est dans la classe Coucou. J’ai indiqué l’endroit critique par des dièses; comme tel, ce n’est manifestement pas la bonne façon d’accéder à cette variable (Toto.a).
Est ce quelqu’un aurait une idée, car ça fait un moment que je tourne en rond.
Merci les pythonistas.