1 | import wx |
---|
2 | class MyStatusBar(wx.StatusBar): |
---|
3 | def __init__(self,*args,**kargs): |
---|
4 | wx.StatusBar.__init__(self, *args,**kargs) |
---|
5 | """ |
---|
6 | Implement statusbar functionalities |
---|
7 | """ |
---|
8 | #Layout of status bar |
---|
9 | self.SetFieldsCount(2) |
---|
10 | width,height = self.GetSize() |
---|
11 | self.gauge = wx.Gauge(self, size=(-1,height-4),style= wx.GA_HORIZONTAL) |
---|
12 | self.SetStatusWidths([-4, -1]) |
---|
13 | rect = self.GetFieldRect(1) |
---|
14 | self.gauge.SetPosition((rect.x+5, rect.y-2)) |
---|
15 | |
---|
16 | self.gauge.Hide() |
---|
17 | ## Current progress value of the bar |
---|
18 | self.progress = 0 |
---|
19 | self.timer = wx.Timer(self,-1) |
---|
20 | self.timer_stop = wx.Timer(self,-1) |
---|
21 | self.thread= None |
---|
22 | self.Bind(wx.EVT_TIMER,self.OnTimer, self.timer) |
---|
23 | self.Bind(wx.EVT_TIMER,self.OnTimer_stop, self.timer_stop) |
---|
24 | self.count=0 |
---|
25 | |
---|
26 | |
---|
27 | def OnTimer_stop(self, evt): |
---|
28 | """Clear the progress bar |
---|
29 | @param evt: wx.EVT_TIMER |
---|
30 | |
---|
31 | """ |
---|
32 | self.count +=1 |
---|
33 | if self.count ==10: |
---|
34 | self.timer_stop.Stop() |
---|
35 | self.gauge.Hide() |
---|
36 | self.SetStatusText( "", 0) |
---|
37 | self.count=0 |
---|
38 | |
---|
39 | |
---|
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 |
---|
46 | if self.timer.IsRunning(): |
---|
47 | self.gauge.Pulse() |
---|
48 | |
---|
49 | |
---|
50 | def set_progress(self): |
---|
51 | """ |
---|
52 | Set the gauge value given the status of a thread |
---|
53 | """ |
---|
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() |
---|
65 | |
---|
66 | self.timer.Stop() |
---|
67 | self.gauge.SetValue(90) |
---|
68 | self.progress =0 |
---|
69 | |
---|
70 | |
---|
71 | def clear_gauge(self, msg=""): |
---|
72 | """ |
---|
73 | Hide the gauge |
---|
74 | """ |
---|
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): |
---|
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 | """ |
---|
89 | if type==None: |
---|
90 | self.SetStatusText(str(msg),0) |
---|
91 | |
---|
92 | else: |
---|
93 | self.SetStatusText(str(msg),0) |
---|
94 | self.thread= thread |
---|
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)) |
---|
101 | |
---|
102 | self.progress +=10 |
---|
103 | if self.progress < self.gauge.GetRange()-20: |
---|
104 | self.gauge.SetValue(int(self.progress)) |
---|
105 | |
---|
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: |
---|
117 | self.gauge.SetValue(int(self.progress)) |
---|
118 | |
---|
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) |
---|
125 | self.timer_stop.Start(3) |
---|
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 | |
---|