Skip to content

Instantly share code, notes, and snippets.

@driscollis
Created July 22, 2017 02:11
Show Gist options
  • Save driscollis/526ea9b0d9672aa421b2862f5fb75bee to your computer and use it in GitHub Desktop.
Save driscollis/526ea9b0d9672aa421b2862f5fb75bee to your computer and use it in GitHub Desktop.
import time
import wx
from threading import Thread
########################################################################
class TestThread(Thread):
"""Test Worker Thread Class."""
#----------------------------------------------------------------------
def __init__(self, dlg):
"""Init Worker Thread Class."""
Thread.__init__(self)
self.dlg = dlg
self.start() # start the thread
#----------------------------------------------------------------------
def run(self):
"""Run Worker Thread."""
# This is the code executing in the new thread.
for i in range(20):
time.sleep(1)
wx.CallAfter(self.dlg.updateProgress)
########################################################################
class MyProgressDialog(wx.Dialog):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Dialog.__init__(self, None, title="Progress")
self.count = 0
self.progress = wx.Gauge(self, range=20)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.progress, 0, wx.EXPAND)
self.SetSizer(sizer)
#----------------------------------------------------------------------
def updateProgress(self):
"""
Update the progress bar
"""
self.count += 1
self.progress.SetValue(self.count)
if self.count >= 20:
self.Destroy()
########################################################################
class MyFrame(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, title="Progress Bar Tutorial")
# Add a panel so it looks the correct on all platforms
panel = wx.Panel(self, wx.ID_ANY)
self.btn = btn = wx.Button(panel, label="Start Thread")
btn.Bind(wx.EVT_BUTTON, self.onButton)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
panel.SetSizer(sizer)
#----------------------------------------------------------------------
def onButton(self, event):
"""
Runs the thread
"""
btn = event.GetEventObject()
btn.Disable()
dlg = MyProgressDialog()
TestThread(dlg)
dlg.ShowModal()
btn.Enable()
#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
app = wx.App(False)
frame = MyFrame()
frame.Show()
app.MainLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment