Add wx.lib.busy

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxPython/Phoenix/trunk@72792 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robin Dunn
2012-10-27 03:16:22 +00:00
parent 06b02a6277
commit 4f57d144d3
2 changed files with 208 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
import imp_unittest, unittest
import wtc
import wx
import os
from wx.lib.busy import BusyInfo
delay = 100
#---------------------------------------------------------------------------
class lib_busy_Tests(wtc.WidgetTestCase):
def test_lib_busy1(self):
with BusyInfo("short message...", self.frame):
wx.MilliSleep(delay*2)
wx.MilliSleep(delay)
self.myYield()
def test_lib_busy2(self):
with BusyInfo("This is my longer short message. Please be patient...", self.frame):
wx.MilliSleep(delay*2)
wx.MilliSleep(delay)
self.myYield()
def test_lib_busy3(self):
busy = BusyInfo("Without using the context manager...", self.frame)
wx.MilliSleep(delay*2)
del busy
wx.MilliSleep(delay)
self.myYield()
def test_lib_busy4(self):
with BusyInfo("Without using the parent window..."):
wx.MilliSleep(delay*2)
wx.MilliSleep(delay)
self.myYield()
def test_lib_busy5(self):
message = """A long message with line breaks:
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit
esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est
laborum."""
with BusyInfo(message, self.frame):
wx.MilliSleep(delay*2)
wx.MilliSleep(delay)
self.myYield()
#---------------------------------------------------------------------------
if __name__ == '__main__':
unittest.main()

148
wx/lib/busy.py Normal file
View File

@@ -0,0 +1,148 @@
#---------------------------------------------------------------------------
# File: busy.py
# Description: A class like wx.BusyInfo but which doesn't take up so much
# space by default and which has a nicer look.
#
# Date: 11-Sept-2012
# Author: Robin Dunn
# Tags: phoenix-port, unittest, documented
#---------------------------------------------------------------------------
"""
A class like wx.BusyInfo but which doesn't take up so much space by default
and which has a nicer look.
"""
import wx
from wx.lib.stattext import GenStaticText as StaticText
#---------------------------------------------------------------------------
class BusyInfo(object):
"""
This class is just like :class:`wx.BusyInfo`, except that its default
size is smaller, (unless the size of the message requires a larger window
size) and the background and foreground colors of the message box can be
set.
Creating an instace of the class witll create an show a window with the
given message, and when the instance is deleted then that window will be
closed. This class also implements the context manager magic methods, so
it can be used with Python's 'with` statement, like this::
with BusyInfo('Please wait...'):
doSomethingThatNeedsWaiting()
"""
def __init__(self, msg, parent=None, bgColour=None, fgColour=None):
"""
Create a new `BusyInfo`.
:param `msg`: a string to be displayed in the BusyInfo window.
:param `parent`: an optional window to be used as the parent of
the `BusyInfo`. If given then the BusyInfo will be centered
over that window, otherwise it will be centered on the screen.
:param `bgColour`: :class:`Colour` to be used for the background
of the `BusyInfo`
:param `fgColour`: :class:`Colour` to be used for the foreground (text)
of the `BusyInfo`
"""
self.frame = _InfoFrame(parent, msg, bgColour, fgColour)
self.frame.Show()
self.frame.Refresh()
self.frame.Update()
def __del__(self):
self.Close()
def Close(self):
"""
Hide and close the busy info box
"""
if self.frame:
self.frame.Hide()
self.frame.Close()
self.frame = None
# Magic methods for using this class as a Context Manager
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.Close()
return False
#---------------------------------------------------------------------------
class _InfoFrame(wx.Frame):
def __init__(self, parent, msg, bgColour=None, fgColour=None):
wx.Frame.__init__(self, parent, style=wx.BORDER_SIMPLE|wx.FRAME_TOOL_WINDOW|wx.STAY_ON_TOP)
bgColour = bgColour if bgColour is not None else wx.Colour(253, 255, 225)
fgColour = fgColour if fgColour is not None else wx.BLACK
panel = wx.Panel(self)
text = StaticText(panel, -1, msg)
for win in [panel, text]:
win.SetCursor(wx.HOURGLASS_CURSOR)
win.SetBackgroundColour(bgColour)
win.SetForegroundColour(fgColour)
size = text.GetBestSize()
self.SetClientSize((size.width + 60, size.height + 40))
panel.SetSize(self.GetClientSize())
text.Center()
self.Center()
#---------------------------------------------------------------------------
if __name__ == '__main__':
def test1(frm):
with BusyInfo("short message...", frm):
wx.Sleep(2)
wx.CallLater(1000, test2, frm)
def test2(frm):
with BusyInfo("This is my longer short message. Please be patient...", frm):
wx.Sleep(2)
wx.CallLater(1000, test3, frm)
def test3(frm):
busy = BusyInfo("Without using the context manager...", frm)
wx.Sleep(2)
del busy
wx.CallLater(1000, test4, frm)
def test4(frm):
with BusyInfo("Without using the parent window..."):
wx.Sleep(2)
wx.CallLater(1000, test5, frm)
def test5(frm):
message = """A long message with line breaks:
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit
esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est
laborum."""
with BusyInfo(message, frm):
wx.Sleep(2)
app = wx.App(False)
frm = wx.Frame(None, title="BusyInfoTest")
wx.CallLater(1000, test1, frm)
frm.Show()
app.MainLoop()