Python Forum
[WxPython] How to use Threads
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[WxPython] How to use Threads
#1
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?
Reply
#2
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()
99 percent of computer problems exists between chair and keyboard.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020