开发者

How do I change variable in a thread?

Here's my situation. Let's say I would like to create a thread that continually prints the number 1. When a button is clicked the val开发者_如何学运维ue should then change to 2. My issue is that I'm unsure how I change a variable in an already running thread. Here's my code:

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()

So the question is, what do I put in the function changeVar that will modify the contents of the variable x that is in the running thread? Thanks in advance!


You can't change local variables.

What you can do is this:

class testThread(Thread):
    def __init__(self, parent):
        self.parent = parent
        Thread.__init__(self)
        self.start()

    def run(self):
        self.value = 1
        while 1:
            print self.value
            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):
        self.the_thread = testThread(self)

    def changeVar(self, event):
        # DO SOMETHING HERE THAT CHANGES 'x' IN THREAD TO 2...
        self.the_thread.value = 2

if __name__ == '__main__': 
    app = wx.App(redirect=False)
    frame = testGUI() 
    frame.Show(True) 
    app.MainLoop()


You don't need threads for that, at all. wx includes a wx.Timer that allows you to call your function with a time interval in the same thread. Chances are that wx includes a function to do what you want, too:

import wx

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 timer", pos=(0,0))
        self.buttonChange = wx.Button(panel, -1, label="Change var", pos=(0,30))
        panel.Bind(wx.EVT_BUTTON, self.startTimer, id=self.buttonStart.GetId())
        panel.Bind(wx.EVT_BUTTON, self.changeVar, id=self.buttonChange.GetId())
        self.value = 1
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.printer, self.timer)

    def startTimer(self, event):
        self.timer.Start(1000) # 1 second

    def printer(self, event):
        print self.value

    def changeVar(self, event):
        self.value = 2

if __name__ == '__main__': 
    app = wx.App(redirect=False)
    frame = testGUI() 
    frame.Show(True) 
    app.MainLoop()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜