Aditi Paul
Aditi Paul

Reputation: 1

How to synchronize wxpython Gauge widget with threads in python?

My python project has multiple threads running. I want to add a gauge widget from the wxpython library to show the progress. I want the gauge to fill until my first thread completes. How do I achieve this? I am using Python 2.7 on Windows.

Upvotes: 0

Views: 487

Answers (3)

Han Programer
Han Programer

Reputation: 43

You can use some simple things remember to import the module:

import os

and then put this on the frame

def __init__(self, *a, **k):
    filename = 'game.exe'


    self.timer = wx.Timer()
    self.Bind(wx.EVT_TIMER, self.OnUpdateGauge, self.timer)
    self.timer.Start()
    self.proc = os.popen(filename) # start the process

def OnUpdateGauge(self, event):
    if self.proc.poll() == None: # return None if still running and 0 if stopped
        self.timer.Stop()
        return
    else:
        '''do something with the gauge'''

hope those help

Upvotes: 0

Yoav Glazner
Yoav Glazner

Reputation: 8066

Use wx.CallAfter

def add_to_the_gauge(value):
    your_gauge.Value += value


...
#in some thread
def some_thread_running():
    wx.CallAfter(add_to_the_gauge, 2)

Upvotes: 1

VZ.
VZ.

Reputation: 22688

You need to post events from your worker thread to the main thread asking it to update the gauge, see this overview.

Upvotes: 0

Related Questions