quickgui を自分好みに改造したついでなので、プログレスバーも簡単に使えればいいかと思って追加してみた。
Updating a wxPython progress bar after calling app.MainLoop() を参考にして、次のコードを quickgui に追加。
def guage(title, func, msg = "processing..."):
import threading
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()
app = wx.PySimpleApp()
dialog = wx.ProgressDialog(title, msg)
start(func, dialog)
dialog.ShowModal()
app.MainLoop()
return True
使うときには、引数として渡してやる関数に更新のロジックを書けばいい。wx.CallAfter の行を入れればいいだけなので、毎回長いコードを書くよりはるかに楽かなと。
# -*- coding: utf-8 -*-
import wx
import quickgui # 改造中のもの
import time
def do_something(dialog):
max_size = 400 # 400回のループを回してみる
step = 100.0 / max_size
count = 0.0
for i in range(max_size):
count += step
wx.CallAfter(dialog.Update, count)
time.sleep(0.1) # 実際の処理をここに
wx.CallAfter(dialog.Destroy)
quickgui.guage("メッセージ処理", do_something, msg="処理中です...")