Python Forum

Full Version: How to use Threads
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I´m developing a GUI with wx.python. In the app, I want to read by modbus some variables in a continuous way. I read that I can use Threads to do this. I have defined my frame as a class and then I call it at the main program. Can I put the funtion of the Thread in an other file and them import it? And if I can, How? Can I define a class an then call to the funtion by a constructor inside the frame class?
You can just us wx.lib.delayedresult startWorker
Here an example
import wx
from wx.lib.delayedresult import startWorker
import time

class HelloForm:
    def __init__(self, *args, **kwargs):
        self.frame = wx.Frame(*args, **kwargs)
        self.panel = wx.Panel(self.frame)
        self.gauge = wx.Gauge(self.panel, size=(300, 20))
        # start thread
        startWorker(self.end_gauge, self.start_gauge)

    def start_gauge(self):
        for x in range(101):
            time.sleep(0.1)
            # send command to wx event safe way
            wx.CallAfter(self.gauge.SetValue, x)

    def end_gauge(self, something):
        self.gauge.SetValue(0)

if __name__ == '__main__':
    app = wx.App()
    hform = HelloForm(None, title='wxpython built in thread')
    hform.frame.Show()
    app.MainLoop()