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 value 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!

I should also mention that this example is just for display purposes and cold be implemented without threading, but I will be using threading to read and write to a controller area network. One thread will be dedicated to polling for data and the other thread will be uses for writing. The write thread will need to transmit data and a constant time interval and the value to be sent will be dependent what the user input in the GUI. So I need some way for the data to be communicated from the button handling function to the thread, hence my dilemma. Sure, I could use global variables, but there must be a better way to do this.

I should also mention that this example is just for display purposes and cold be implemented without threading, but I will be using threading to read and write to a controller area network. One thread will be dedicated to polling for data and the other thread will be uses for writing. The write thread will need to transmit data and a constant time interval and the value to be sent will be dependent what the user input in the GUI. So I need some way for the data to be communicated from the button handling function to the thread, hence my dilemma. Sure, I could use global variables, but there must be a better way to do this.

Don't you think the button handling function could append the data to a Queue.Queue and the writing thread could take the data at the other end of the queue and process it ?

Edited 6 Years Ago by Gribouillis: n/a

This question has already been answered. Start a new discussion instead.