1 | import wx |
---|
2 | class MyStatusBar(wx.StatusBar): |
---|
3 | def __init__(self,*args,**kargs): |
---|
4 | wx.StatusBar.__init__(self, *args,**kargs) |
---|
5 | #Layout of status bar |
---|
6 | self.SetFieldsCount(2) |
---|
7 | width,height = self.GetSize() |
---|
8 | self.gauge = wx.Gauge(self, size=(-1,height-4),style= wx.GA_HORIZONTAL) |
---|
9 | self.SetStatusWidths([-4, -1]) |
---|
10 | rect = self.GetFieldRect(1) |
---|
11 | self.gauge.SetPosition((rect.x+5, rect.y-2)) |
---|
12 | |
---|
13 | self.gauge.Hide() |
---|
14 | |
---|
15 | #Progess |
---|
16 | |
---|
17 | self.progress = 0 # Current progress value of the bar |
---|
18 | self.timer = wx.Timer(self,-1) |
---|
19 | self.thread= None |
---|
20 | self.Bind(wx.EVT_TIMER,self.OnTimer, self.timer) |
---|
21 | |
---|
22 | def OnTimer(self, evt): |
---|
23 | """Update the progress bar while the timer is running |
---|
24 | @param evt: wx.EVT_TIMER |
---|
25 | |
---|
26 | """ |
---|
27 | # Check stop flag that can be set from non main thread |
---|
28 | self.gauge.Pulse() |
---|
29 | |
---|
30 | def set_progress(self): |
---|
31 | self.gauge.Show(True) |
---|
32 | if self.timer.IsRunning(): |
---|
33 | while(self.thread.isrunning()): |
---|
34 | self.progress += 1 |
---|
35 | |
---|
36 | # Update the Rqnge if it has changed |
---|
37 | #if self.range >= 0 and self.range != self.gauge.GetRange(): |
---|
38 | # self.gauge.SetRange(self.range) |
---|
39 | |
---|
40 | # Update the progress value if it is less than the range |
---|
41 | if self.progress < self.gauge.GetRange()-20: |
---|
42 | self.gauge.SetValue(int(self.progress)) |
---|
43 | else: |
---|
44 | self.gauge.SetValue(70) |
---|
45 | self.progress =0 |
---|
46 | self.timer.Stop() |
---|
47 | #self.gauge.Hide() |
---|
48 | self.timer.Stop() |
---|
49 | self.gauge.SetValue(90) |
---|
50 | self.progress =0 |
---|
51 | |
---|
52 | def clear_gauge(self, msg=""): |
---|
53 | self.timer.Stop() |
---|
54 | self.SetStatusText( str(msg), 0) |
---|
55 | self.progress =0 |
---|
56 | self.gauge.SetValue(0) |
---|
57 | self.gauge.Hide() |
---|
58 | |
---|
59 | def set_status(self, type=None,msg="", thread=None): |
---|
60 | if type==None: |
---|
61 | self.SetStatusText(str(msg),0) |
---|
62 | |
---|
63 | else: |
---|
64 | self.thread= thread |
---|
65 | #if self.thread !=None: |
---|
66 | self.gauge.Show(True) |
---|
67 | if type.lower()=="start": |
---|
68 | self.timer.Stop() |
---|
69 | self.SetStatusText( str(msg), 0) |
---|
70 | self.progress +=10 |
---|
71 | self.gauge.SetValue(int(self.progress)) |
---|
72 | #self.timer.Start(1000) |
---|
73 | #print "went here" |
---|
74 | #self.set_progress() |
---|
75 | self.progress +=10 |
---|
76 | if self.progress < self.gauge.GetRange()-20: |
---|
77 | self.gauge.SetValue(int(self.progress)) |
---|
78 | if type.lower()=="progress": |
---|
79 | self.timer.Start(100) |
---|
80 | self.SetStatusText( str(msg), 0) |
---|
81 | self.gauge.Pulse() |
---|
82 | print "in progress" |
---|
83 | |
---|
84 | if type.lower()=="update": |
---|
85 | |
---|
86 | self.timer.Stop() |
---|
87 | self.SetStatusText( str(msg), 0) |
---|
88 | self.progress +=10 |
---|
89 | if self.progress < self.gauge.GetRange()-20: |
---|
90 | self.gauge.SetValue(int(self.progress)) |
---|
91 | if type.lower()=="stop": |
---|
92 | print "it is complete" |
---|
93 | self.gauge.Show(True) |
---|
94 | self.timer.Stop() |
---|
95 | self.SetStatusText( str(msg), 0) |
---|
96 | self.progress =0 |
---|
97 | self.gauge.SetValue(90) |
---|
98 | |
---|
99 | |
---|
100 | |
---|
101 | |
---|
102 | if __name__ == "__main__": |
---|
103 | app = wx.PySimpleApp() |
---|
104 | frame= wx.Frame(None,wx.ID_ANY,'test frame') |
---|
105 | statusBar= MyStatusBar(frame,wx.ID_ANY) |
---|
106 | frame.SetStatusBar(statusBar) |
---|
107 | frame.Show(True) |
---|
108 | statusBar.SetStatusText("status text..") |
---|
109 | statusBar.set_status( "progress","progessing") |
---|
110 | |
---|
111 | |
---|
112 | app.MainLoop() |
---|
113 | |
---|