Voici ma situation. Disons que je voudrais créer un thread qui imprime continuellement le numéro 1. Quand un bouton est cliqué, la valeur devrait alors changer à 2. Mon problème est que je ne sais pas comment je change une variable dans un thread en cours d'exécution. Voici mon code:Comment changer une variable dans un thread?
import wx
from threading import Thread
import time
class testThread(Thread):
def __init__(self, parent):
self.parent = parent
Thread.__init__(self)
self.start()
def run(self):
while 1:
x = 1
print x
time.sleep(1)
class testGUI(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "Test", size=(500,270))
panel = wx.Panel(self, -1)
self.buttonStart = wx.Button(panel, -1, label="Start thread", pos=(0,0))
self.buttonChange = wx.Button(panel, -1, label="Change var", pos=(0,30))
panel.Bind(wx.EVT_BUTTON, self.startThread, id=self.buttonStart.GetId())
panel.Bind(wx.EVT_BUTTON, self.changeVar, id=self.buttonChange.GetId())
def startThread(self, event):
testThread(self)
def changeVar(self, event):
# DO SOMETHING HERE THAT CHANGES 'x' IN THREAD TO 2...
pass
if __name__ == '__main__':
app = wx.App(redirect=False)
frame = testGUI()
frame.Show(True)
app.MainLoop()
La question est, qu'est-ce que je mets dans la fonction changeVar qui modifiera le contenu de la variable x qui est dans le fil conducteur? Merci d'avance!
Génial, merci THC4k, cela fonctionne parfaitement! – radian