-1

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.

otus
  • 5,572
  • 1
  • 34
  • 48
tk_y1275963
  • 588
  • 6
  • 13
  • Your question has neither [magic methods](http://stackoverflow.com/questions/1090620/special-magic-methods-in-python) nor [classes inside classes](http://stackoverflow.com/questions/1765677/python-nested-classes-scope), so I'm editing your question. – otus Jun 08 '14 at 06:54
  • That's just creating a new instance of `Camera` class using named parameter. There's nothing magic about it. – Kien Truong Jun 08 '14 at 06:59

1 Answers1

0

What you are seeing is a use of keyword arguments.

return Camera(display = self.display)

That calls the constructor for Camera to create a new object of that class, passing self.display as the keyword argument display.

According to PEP 8 (the style guide) you are not supposed to use spaces around the = sign, though, so the following would be better stylistically, making it clearer this is not an equality test:

return Camera(display=self.display)

If you don't understand the rest (e.g. the self.display part), you really need to read the basics of classes in Python first.

otus
  • 5,572
  • 1
  • 34
  • 48
  • Ah, Thanks.. I just use the python as a substitute to matlab. So I am not quite familiar with the Class parts.. – tk_y1275963 Jun 08 '14 at 07:08
  • But could you please tell me how the whole thing works.. I mean, how did the Textdisplay inside the MainWindow get refreshed? Because the thread is controlled by the Camera and no argument has been give to the Textdisplay – tk_y1275963 Jun 08 '14 at 07:14
  • @tk_y1275963, the camera constructor got the TextDisplay as an argument and can access its methods and variables. It does access it in `self.capture_thread.display = self.display`. – otus Jun 08 '14 at 07:21