[dd66fbd] | 1 | import wx |
---|
| 2 | class MyStatusBar(wx.StatusBar): |
---|
| 3 | def __init__(self,*args,**kargs): |
---|
| 4 | wx.StatusBar.__init__(self, *args,**kargs) |
---|
[12aa9b5] | 5 | """ |
---|
| 6 | Implement statusbar functionalities |
---|
| 7 | """ |
---|
[dd66fbd] | 8 | #Layout of status bar |
---|
[2cd9153] | 9 | self.SetFieldsCount(2) |
---|
[dd66fbd] | 10 | width,height = self.GetSize() |
---|
[2cd9153] | 11 | self.gauge = wx.Gauge(self, size=(-1,height-4),style= wx.GA_HORIZONTAL) |
---|
| 12 | self.SetStatusWidths([-4, -1]) |
---|
[dd66fbd] | 13 | rect = self.GetFieldRect(1) |
---|
[282a3e7] | 14 | self.gauge.SetPosition((rect.x+5, rect.y-2)) |
---|
[2cd9153] | 15 | |
---|
[dd66fbd] | 16 | self.gauge.Hide() |
---|
[12aa9b5] | 17 | ## Current progress value of the bar |
---|
| 18 | self.progress = 0 |
---|
[dd66fbd] | 19 | self.timer = wx.Timer(self,-1) |
---|
[858f2ae5] | 20 | self.timer_stop = wx.Timer(self,-1) |
---|
[dd66fbd] | 21 | self.thread= None |
---|
[ef628a1] | 22 | self.Bind(wx.EVT_TIMER,self.OnTimer, self.timer) |
---|
[858f2ae5] | 23 | self.Bind(wx.EVT_TIMER,self.OnTimer_stop, self.timer_stop) |
---|
| 24 | self.count=0 |
---|
[12aa9b5] | 25 | |
---|
[dd66fbd] | 26 | |
---|
[858f2ae5] | 27 | def OnTimer_stop(self, evt): |
---|
[12aa9b5] | 28 | """Clear the progress bar |
---|
[858f2ae5] | 29 | @param evt: wx.EVT_TIMER |
---|
| 30 | |
---|
| 31 | """ |
---|
| 32 | self.count +=1 |
---|
[d9dbb76] | 33 | if self.count <=10: |
---|
[858f2ae5] | 34 | self.timer_stop.Stop() |
---|
| 35 | self.gauge.Hide() |
---|
[60c1943] | 36 | #self.SetStatusText( "", 0) |
---|
[858f2ae5] | 37 | self.count=0 |
---|
[12aa9b5] | 38 | |
---|
| 39 | |
---|
[dd66fbd] | 40 | def OnTimer(self, evt): |
---|
| 41 | """Update the progress bar while the timer is running |
---|
| 42 | @param evt: wx.EVT_TIMER |
---|
| 43 | |
---|
| 44 | """ |
---|
| 45 | # Check stop flag that can be set from non main thread |
---|
[858f2ae5] | 46 | if self.timer.IsRunning(): |
---|
| 47 | self.gauge.Pulse() |
---|
| 48 | |
---|
[dd66fbd] | 49 | |
---|
| 50 | def set_progress(self): |
---|
[12aa9b5] | 51 | """ |
---|
| 52 | Set the gauge value given the status of a thread |
---|
| 53 | """ |
---|
[dd66fbd] | 54 | self.gauge.Show(True) |
---|
| 55 | if self.timer.IsRunning(): |
---|
| 56 | while(self.thread.isrunning()): |
---|
| 57 | self.progress += 1 |
---|
| 58 | # Update the progress value if it is less than the range |
---|
| 59 | if self.progress < self.gauge.GetRange()-20: |
---|
| 60 | self.gauge.SetValue(int(self.progress)) |
---|
| 61 | else: |
---|
| 62 | self.gauge.SetValue(70) |
---|
| 63 | self.progress =0 |
---|
| 64 | self.timer.Stop() |
---|
[12aa9b5] | 65 | |
---|
[dd66fbd] | 66 | self.timer.Stop() |
---|
| 67 | self.gauge.SetValue(90) |
---|
| 68 | self.progress =0 |
---|
| 69 | |
---|
[12aa9b5] | 70 | |
---|
[dd66fbd] | 71 | def clear_gauge(self, msg=""): |
---|
[12aa9b5] | 72 | """ |
---|
| 73 | Hide the gauge |
---|
| 74 | """ |
---|
[dd66fbd] | 75 | self.timer.Stop() |
---|
| 76 | self.SetStatusText( str(msg), 0) |
---|
| 77 | self.progress =0 |
---|
| 78 | self.gauge.SetValue(0) |
---|
| 79 | self.gauge.Hide() |
---|
| 80 | |
---|
| 81 | def set_status(self, type=None,msg="", thread=None): |
---|
[12aa9b5] | 82 | """ |
---|
| 83 | Update the status bar . |
---|
| 84 | @param type: type of message send. |
---|
| 85 | type must be in ["start","progress","update","stop"] |
---|
| 86 | @param msg: the message itself as string |
---|
| 87 | @param thread: if updatting using a thread status |
---|
| 88 | """ |
---|
[dd66fbd] | 89 | if type==None: |
---|
| 90 | self.SetStatusText(str(msg),0) |
---|
| 91 | |
---|
| 92 | else: |
---|
[f706f393] | 93 | self.SetStatusText(str(msg),0) |
---|
[dd66fbd] | 94 | self.thread= thread |
---|
[282a3e7] | 95 | self.gauge.Show(True) |
---|
| 96 | if type.lower()=="start": |
---|
| 97 | self.timer.Stop() |
---|
| 98 | self.SetStatusText( str(msg), 0) |
---|
| 99 | self.progress +=10 |
---|
| 100 | self.gauge.SetValue(int(self.progress)) |
---|
[12aa9b5] | 101 | |
---|
[282a3e7] | 102 | self.progress +=10 |
---|
| 103 | if self.progress < self.gauge.GetRange()-20: |
---|
| 104 | self.gauge.SetValue(int(self.progress)) |
---|
[12aa9b5] | 105 | |
---|
[282a3e7] | 106 | if type.lower()=="progress": |
---|
| 107 | self.timer.Start(100) |
---|
| 108 | self.SetStatusText( str(msg), 0) |
---|
| 109 | self.gauge.Pulse() |
---|
| 110 | |
---|
| 111 | if type.lower()=="update": |
---|
| 112 | |
---|
| 113 | self.timer.Stop() |
---|
| 114 | self.SetStatusText( str(msg), 0) |
---|
| 115 | self.progress +=10 |
---|
| 116 | if self.progress < self.gauge.GetRange()-20: |
---|
[ef628a1] | 117 | self.gauge.SetValue(int(self.progress)) |
---|
[700f9b4] | 118 | |
---|
[dd66fbd] | 119 | if type.lower()=="stop": |
---|
| 120 | self.gauge.Show(True) |
---|
| 121 | self.timer.Stop() |
---|
| 122 | self.SetStatusText( str(msg), 0) |
---|
| 123 | self.progress =0 |
---|
| 124 | self.gauge.SetValue(90) |
---|
[858f2ae5] | 125 | self.timer_stop.Start(3) |
---|
[dd66fbd] | 126 | |
---|
| 127 | |
---|
| 128 | |
---|
| 129 | if __name__ == "__main__": |
---|
| 130 | app = wx.PySimpleApp() |
---|
| 131 | frame= wx.Frame(None,wx.ID_ANY,'test frame') |
---|
| 132 | statusBar= MyStatusBar(frame,wx.ID_ANY) |
---|
| 133 | frame.SetStatusBar(statusBar) |
---|
| 134 | frame.Show(True) |
---|
| 135 | statusBar.SetStatusText("status text..") |
---|
| 136 | statusBar.set_status( "progress","progessing") |
---|
| 137 | |
---|
| 138 | |
---|
| 139 | app.MainLoop() |
---|
| 140 | |
---|