Skip to content Skip to sidebar Skip to footer

Updating A Wxpython Progress Bar After Calling App.mainloop()

I have a python script that performs a calculation, and I have created a class for a pop-up wxPython progress bar. Currently I have: app=wx.App() progress = ProgressBar() app.MainL

Solution 1:

You should run your logic in a background thread and use wx.CallAfter to periodically update the GUI. CallAfter will invoke the provided function on the GUI thread, so it is safe to make GUI calls.

import wx
import threading
import time

def do_stuff(dialog): # put your logic here
    for i in range(101):
        wx.CallAfter(dialog.Update, i)
        time.sleep(0.1)
    wx.CallAfter(dialog.Destroy)

def start(func, *args): # helper method to run a function in another thread
    thread = threading.Thread(target=func, args=args)
    thread.setDaemon(True)
    thread.start()

def main():
    app = wx.PySimpleApp()
    dialog = wx.ProgressDialog('Doing Stuff', 'Please wait...')
    start(do_stuff, dialog)
    dialog.ShowModal()
    app.MainLoop()

if __name__ == '__main__':
    main()

Post a Comment for "Updating A Wxpython Progress Bar After Calling App.mainloop()"