Guys, I recently wrote a couple of codes about the TraitUi and one example is quite confusing to me.
Here is the whole example:
__author__ = 'tk'
from threading import Thread
from time import sleep
from traits.api import *
from traitsui.api import View, Item, ButtonEditor
class TextDisplay(HasTraits):
string = String()
view = View( Item('string', 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(.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'))
def _start_stop_capture_fired(self):
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)
MainWindow().configure_traits()
My question is about the last class, the one called MainWindow. It defined a variable Camera:
return Camera(display = self.display)
The code above really confuses me. Is there any one who can help me with this? I am not quite familiar with the Object-Oriented Programming in Python nor did I do with the Magic Method In python. Could you please kindly explained to me what has this statement does? Because there are two variable called display, I just got lost.