| 1 | |
| 2 | import time |
| 3 | import wx |
| 4 | |
| 5 | #--------------------------------------------------------------------------- |
| 6 | |
| 7 | class CustomStatusBar(wx.StatusBar): |
| 8 | def __init__(self, parent, log): |
| 9 | wx.StatusBar.__init__(self, parent, -1) |
| 10 | |
| 11 | # This status bar has three fields |
| 12 | self.SetFieldsCount(3) |
| 13 | # Sets the three fields to be relative widths to each other. |
| 14 | self.SetStatusWidths([-2, -1, -2]) |
| 15 | self.log = log |
| 16 | self.sizeChanged = False |
| 17 | self.Bind(wx.EVT_SIZE, self.OnSize) |
| 18 | self.Bind(wx.EVT_IDLE, self.OnIdle) |
| 19 | |
| 20 | # Field 0 ... just text |
| 21 | self.SetStatusText("A Custom StatusBar...", 0) |
| 22 | |
| 23 | # This will fall into field 1 (the second field) |
| 24 | self.cb = wx.CheckBox(self, 1001, "toggle clock") |
| 25 | self.Bind(wx.EVT_CHECKBOX, self.OnToggleClock, self.cb) |
| 26 | self.cb.SetValue(True) |
| 27 | |
| 28 | # set the initial position of the checkbox |
| 29 | self.Reposition() |
| 30 | |
| 31 | # We're going to use a timer to drive a 'clock' in the last |
| 32 | # field. |
| 33 | self.timer = wx.PyTimer(self.Notify) |
| 34 | self.timer.Start(1000) |
| 35 | self.Notify() |
| 36 | |
| 37 | |
| 38 | # Handles events from the timer we started in __init__(). |
| 39 | # We're using it to drive a 'clock' in field 2 (the third field). |
| 40 | def Notify(self): |
| 41 | t = time.localtime(time.time()) |
| 42 | st = time.strftime("%d-%b-%Y %I:%M:%S", t) |
| 43 | self.SetStatusText(st, 2) |
| 44 | self.log.WriteText("tick...\n") |
| 45 | |
| 46 | |
| 47 | # the checkbox was clicked |
| 48 | def OnToggleClock(self, event): |
| 49 | if self.cb.GetValue(): |
| 50 | self.timer.Start(1000) |
| 51 | self.Notify() |
| 52 | else: |
| 53 | self.timer.Stop() |
| 54 | |
| 55 | |
| 56 | def OnSize(self, evt): |
| 57 | self.Reposition() # for normal size events |
| 58 | |
| 59 | # Set a flag so the idle time handler will also do the repositioning. |
| 60 | # It is done this way to get around a buglet where GetFieldRect is not |
| 61 | # accurate during the EVT_SIZE resulting from a frame maximize. |
| 62 | self.sizeChanged = True |
| 63 | |
| 64 | |
| 65 | def OnIdle(self, evt): |
| 66 | if self.sizeChanged: |
| 67 | self.Reposition() |
| 68 | |
| 69 | |
| 70 | # reposition the checkbox |
| 71 | def Reposition(self): |
| 72 | rect = self.GetFieldRect(1) |
| 73 | self.cb.SetPosition((rect.x+2, rect.y+2)) |
| 74 | self.cb.SetSize((rect.width-4, rect.height-4)) |
| 75 | self.sizeChanged = False |
| 76 | |
| 77 | |
| 78 | |
| 79 | class TestCustomStatusBar(wx.Frame): |
| 80 | def __init__(self, parent, log): |
| 81 | wx.Frame.__init__(self, parent, -1, 'Test Custom StatusBar') |
| 82 | |
| 83 | self.sb = CustomStatusBar(self, log) |
| 84 | self.SetStatusBar(self.sb) |
| 85 | tc = wx.TextCtrl(self, -1, "", style=wx.TE_READONLY|wx.TE_MULTILINE) |
| 86 | |
| 87 | self.SetSize((640, 480)) |
| 88 | self.Bind(wx.EVT_CLOSE, self.OnCloseWindow) |
| 89 | |
| 90 | def OnCloseWindow(self, event): |
| 91 | self.sb.timer.Stop() |
| 92 | del self.sb.timer |
| 93 | self.Destroy() |
| 94 | |
| 95 | #--------------------------------------------------------------------------- |
| 96 | |
| 97 | class TestPanel(wx.Panel): |
| 98 | def __init__(self, parent, log): |
| 99 | self.log = log |
| 100 | wx.Panel.__init__(self, parent, -1) |
| 101 | |
| 102 | b = wx.Button(self, -1, "Show the StatusBar sample", (50,50)) |
| 103 | self.Bind(wx.EVT_BUTTON, self.OnButton, b) |
| 104 | |
| 105 | |
| 106 | def OnButton(self, evt): |
| 107 | win = TestCustomStatusBar(self, self.log) |
| 108 | win.Show(True) |
| 109 | |
| 110 | #--------------------------------------------------------------------------- |
| 111 | |
| 112 | |
| 113 | def runTest(frame, nb, log): |
| 114 | win = TestPanel(nb, log) |
| 115 | return win |
| 116 | |
| 117 | |
| 118 | #--------------------------------------------------------------------------- |
| 119 | |
| 120 | |
| 121 | overview = """\ |
| 122 | A status bar is a narrow window that can be placed along the bottom of |
| 123 | a frame to give small amounts of status information. It can contain |
| 124 | one or more fields, one or more of which can be variable length |
| 125 | according to the size of the window. |
| 126 | |
| 127 | This example demonstrates how to create a custom status bar with actual |
| 128 | gadgets embedded in it. In this case, the first field is just plain text, |
| 129 | The second one has a checkbox that enables the timer, and the third |
| 130 | field has a clock that shows the current time when it is enabled. |
| 131 | |
| 132 | """ |
| 133 | |
| 134 | |
| 135 | if __name__ == '__main__': |
| 136 | import sys,os |
| 137 | import run |
| 138 | run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:]) |