1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 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()
|