1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
# traits_thread.py
from threading import Thread
from time import sleep
from traits.api import Button, HasTraits, Instance, observe, Str
from traitsui.api import View, Item
class TextDisplay(HasTraits):
string = Str()
view = View(Item('string', show_label=False, springy=True, style='custom'))
class CaptureThread(Thread):
def run(self):
self.display.string = 'Camera started\n' + self.display.string
n_img = 0
while not self.wants_abort:
sleep(0.5)
n_img += 1
self.display.string = (
'%d image captured\n' % n_img + self.display.string
)
self.display.string = 'Camera stopped\n' + self.display.string
class Camera(HasTraits):
start_stop_capture = Button()
display = Instance(TextDisplay)
capture_thread = Instance(CaptureThread)
view = View(Item('start_stop_capture', show_label=False))
@observe('start_stop_capture')
def _on_start_stop_capture(self, event):
if self.capture_thread and self.capture_thread.isAlive():
self.capture_thread.wants_abort = True
else:
self.capture_thread = CaptureThread()
self.capture_thread.wants_abort = False
self.capture_thread.display = self.display
self.capture_thread.start()
class MainWindow(HasTraits):
display = Instance(TextDisplay, ())
camera = Instance(Camera)
def _camera_default(self):
return Camera(display=self.display)
view = View('display', 'camera', style="custom", resizable=True)
if __name__ == '__main__':
MainWindow().configure_traits()
|