SHOW:
|
|
- or go back to the newest paste.
1 | import wx | |
2 | import wx.animate | |
3 | import os | |
4 | import time | |
5 | import multiprocessing | |
6 | ||
7 | ||
8 | class processModal(): | |
9 | def __init__(self, parentFrame, useProgress=None, objWithProgressAttribute=None, progressAttributeName=None): | |
10 | - | #result_queue = multiprocessing.Queue() |
10 | + | |
11 | - | #self.modal = |
11 | + | |
12 | - | #montecarlos = [MonteCarlo(result_queue, f,fargs) for fargs in farglist] |
12 | + | |
13 | - | #jobs = [multiprocessing.Process(mc) for mc in montecarlos] |
13 | + | |
14 | self.progressAttributeName = progressAttributeName | |
15 | ||
16 | event = multiprocessing.Event() | |
17 | _q = multiprocessing.Queue() | |
18 | self.event=event | |
19 | self._q = _q | |
20 | proc = multiprocessing.Process(target=self.openPopupInNewProcess, args=(_q, event)) | |
21 | proc.start() | |
22 | ||
23 | def openPopupInNewProcess(self, _q, _event): | |
24 | app = wx.App(False) | |
25 | print "here1" | |
26 | - | #for job in jobs: job.start() |
26 | + | |
27 | - | #for job in jobs: job.join() |
27 | + | |
28 | - | #results = [result_queue.get() for mc in montecarlos] |
28 | + | |
29 | ||
30 | def UnShow(self, parentFrame): | |
31 | print 'calling set evet' | |
32 | self.event.set() | |
33 | parentFrame.Enable(True) | |
34 | ||
35 | def UpdateProgress(self): | |
36 | curVal = getattr(self.objWithProgressAttribute, self.progressAttributeName) | |
37 | self._q.put(curVal) | |
38 | ||
39 | ||
40 | class PopupModal(wx.Frame): | |
41 | def __init__(self, _q, _event, useProgress=None, title="Busy, please wait."): | |
42 | #if parent: | |
43 | # wx.Frame.__init__(self, parent, -1, title=title, size=(200,200), style=wx.FRAME_FLOAT_ON_PARENT | wx.CAPTION | wx.FRAME_TOOL_WINDOW) | |
44 | wx.Frame.__init__(self, None, -1, title=title, size=(200,200), style=wx.CAPTION | wx.FRAME_TOOL_WINDOW) | |
45 | self.TestPanel = wx.Panel(self, -1) | |
46 | self._event = _event | |
47 | self._q = _q | |
48 | self.Hsizer = wx.BoxSizer(wx.HORIZONTAL) | |
49 | self.Hsizer.AddStretchSpacer(1) | |
50 | ani = wx.animate.Animation(os.path.join(os.path.dirname(__file__), "Spinning_wheel_throbber.gif")) | |
51 | self.beachBall = wx.animate.AnimationCtrl(self.TestPanel, -1, ani) | |
52 | self.beachBall.SetUseWindowBackgroundColour() | |
53 | self.beachBall.Play() | |
54 | if useProgress: | |
55 | ||
56 | self.progressGauge = wx.Gauge(self.TestPanel, range=100) | |
57 | self.progressGauge.Show(False) | |
58 | ||
59 | self.Hsizer.AddF(self.beachBall, wx.SizerFlags(0).Center()) | |
60 | ||
61 | self.Hsizer.AddStretchSpacer(1) | |
62 | self.TestPanel.SetSizerAndFit(self.Hsizer) | |
63 | ||
64 | ||
65 | updateTime = wx.Timer(self, -1) | |
66 | updateTime.Start(100, oneShot = False) | |
67 | #bind the timer to the re-enable function | |
68 | self.Bind(wx.EVT_TIMER, self.UpdateTimerFunc) | |
69 | self.Show() | |
70 | self.Refresh() | |
71 | print "after!" | |
72 | ||
73 | ||
74 | def Show(self): | |
75 | #self.CenterOnParent() | |
76 | #self.GetParent().Enable(False) | |
77 | wx.Frame.Show(self) | |
78 | self.Raise() | |
79 | ||
80 | ||
81 | def UnShow(self): | |
82 | #self.GetParent().Enable(True) | |
83 | self.Destroy() | |
84 | ||
85 | def UpdateTimerFunc(self): | |
86 | print "in timer" | |
87 | if self._event.is_set(): | |
88 | print "after event set" | |
89 | self.UnShow() | |
90 | else: | |
91 | try: | |
92 | curVal = self._q.get(timeout=0.1) | |
93 | print "curval %s" % curVal | |
94 | self.UpdateProgress(curVal) | |
95 | except Exception as e: #multiprocessing.Queue.Empty: | |
96 | print "idling" | |
97 | print e | |
98 | pass | |
99 | #wx.Yield() | |
100 | #time.sleep(0.1) | |
101 | ||
102 | #self.UnShow() | |
103 | ||
104 | #wx.CallAfter(self.UpdateProgress, curVal) | |
105 | #self.UpdateProgress(curVal) | |
106 | ||
107 | #print "in UpdateTimerFunc %s" % curVal | |
108 | #wx.Yield() | |
109 | #time.sleep(0.1) | |
110 | ||
111 | ||
112 | def UpdateProgress(self, val): | |
113 | if hasattr(self, "beachBall"): | |
114 | if self.beachBall.IsShown(): | |
115 | self.beachBall.Stop() | |
116 | self.beachBall.Show(False) | |
117 | self.Hsizer.Remove(self.beachBall) | |
118 | self.Hsizer.AddF(self.progressGauge, wx.SizerFlags(0).Center()) | |
119 | self.TestPanel.SetSizerAndFit(self.Hsizer) | |
120 | self.progressGauge.Show(True) | |
121 | self.ctrl.SetValue(val) | |
122 | ||
123 | ||
124 | import os | |
125 | ||
126 | ||
127 | import time | |
128 | ||
129 | ||
130 | class TestPanel(wx.Panel): | |
131 | def __init__(self, parent): | |
132 | ||
133 | wx.Panel.__init__(self, parent, -1) | |
134 | ||
135 | Hsizer = wx.BoxSizer(wx.HORIZONTAL) | |
136 | Hsizer.AddStretchSpacer(1) | |
137 | for name in GIFNames: | |
138 | ani = wx.animate.Animation(name) | |
139 | ctrl = wx.animate.AnimationCtrl(self, -1, ani) | |
140 | ctrl.SetUseWindowBackgroundColour() | |
141 | ctrl.Play() | |
142 | Hsizer.AddF(ctrl, wx.SizerFlags(0).Center()) | |
143 | ||
144 | Hsizer.AddStretchSpacer(1) | |
145 | self.SetSizerAndFit(Hsizer) | |
146 | ||
147 | ||
148 | class faker(): | |
149 | PROGRESS=0 | |
150 | def longTime(self, a,b, updateFunc=None): | |
151 | print "a %s, b %s" % (a, b) | |
152 | for i in range(10): | |
153 | time.sleep(1) | |
154 | self.PROGRESS+=10 | |
155 | if updateFunc: | |
156 | print "calling updateFunc from faker" | |
157 | updateFunc() | |
158 | - | for i in range(5): |
158 | + | |
159 | - | time.sleep(.5) |
159 | + | |
160 | ||
161 | ||
162 | class needsModalToPopup(wx.Frame): | |
163 | def __init__(self): | |
164 | wx.Frame.__init__(self, parent=None, title="animate test", size=(800, 600)) | |
165 | panel = wx.Panel(self, -1) | |
166 | sizer = wx.BoxSizer(wx.VERTICAL) | |
167 | btn = wx.Button(panel, -1, "Start long-running task") | |
168 | sizer.Add(btn) | |
169 | ||
170 | panel.SetSizer(sizer) | |
171 | panel.Layout() | |
172 | ||
173 | btn.Bind(wx.EVT_BUTTON, self.OnBlock) | |
174 | ||
175 | def OnBlock(self, event): | |
176 | f = faker() | |
177 | dlg = processModal(self, useProgress=None, objWithProgressAttribute=f, progressAttributeName="PROGRESS") | |
178 | print f.longTime(5, 'a', dlg.UpdateProgress) | |
179 | dlg.UnShow(self) | |
180 | ||
181 | ||
182 | if __name__ == "__main__": | |
183 | app = wx.App(False) | |
184 | frame = needsModalToPopup() | |
185 | frame.Show() | |
186 | app.MainLoop() |