Документ взят из кэша поисковой машины. Адрес оригинального документа : http://www.apo.nmsu.edu/35m_operations/TUI/Scripts/ScriptingTutorial/Basics.html
Дата изменения: Sat Sep 6 02:16:02 2014
Дата индексирования: Sun Apr 10 06:31:45 2016
Кодировка:
TUI:Scripts:Scripting Tutorial:Basics

Basics - Hello script

Let's start with the traditional greeting. Create a text file "hello.py" in your home directory (or anywhere convenient) containing the following text. Use tabs for indentation:


def run(sr):
    sr.showMsg("Hello")
    yield sr.waitMS(1000)

Start TUI (if it is not already running) and load your script using Open... in the Scripts menu. A new script window should appear; press "Start" to run your script. This causes the status bar to show "Hello" for 1 second, after which your script ends and the status bar shows "Done".

To reload a script (i.e. after modifying it), select Reload from the contextual pop-up menu for the status bar or any of the buttons along the bottom (Start, etc.). This is useful for developing scripts: you can modify the script in your favorite editor, then reload it in TUI and try it out.

What's With "yield"?

Whenever your script calls an sr.wait... function (i.e. wants to wait for anything), it must use yield, as in:

    yield sr.wait...(...)

This is a painful, but it could be worse. Most languages would force you to break your script into many small functions, each of which would have to be registered as a separate callback function. That style of programming is fine for user interfaces but is needlessly complicated for scripts.

If you forget the "yield", your script will plow ahead instead of waiting, which is a recipe for trouble. However, TUI will catch this problem the next time you call an sr.wait... function, at which point TUI will kill your script, print a message to the status bar and print details to the error log.