如何更改线程中的变量?

时间:2010-12-09 21:45:35

标签: python wxpython

这是我的情况。假设我想创建一个不断打印数字1的线程。当单击一个按钮时,该值应该更改为2.我的问题是我不确定如何在已经运行的线程中更改变量。这是我的代码:

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

所以问题是,我将在函数changeVar中添加什么来修改正在运行的线程中的变量x的内容?提前致谢!

2 个答案:

答案 0 :(得分:2)

您无法更改局部变量。

你可以做的是:

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

答案 1 :(得分:1)

根本不需要线程。 wx包含一个wx.Timer,允许您在同一个线程中以一个时间间隔调用您的函数。有可能wx包含一个功能来做你想做的事情:

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