Documentation > Python
Hello World!
Let's start with the traditional 'Hello World!' example.
What will you learn?
You will write your first Python program and learn how to run it nControl.
You will understand the basic structure of a statement in Python.
What will your program do?
It will customize the behavior of a tile.
It will print the text Hello World! every time you press it.
What will you need?
nControl 2018.0 or higher.
Writing the Program
nControl is designed to control train layouts using on screen tiles. The behavior of each tile can be fully customized with Python scripts.
Let's start by programming a generic tile to print the text Hello World! on our screen.
Start the nControl software. Then go to Configuration Mode, create a new generic tile and open the code editor.
In the editor, type the text self.print('Hello World!'). Close the editor and the Edit Generic Tile Properties dialog box. You should now see a generic tile in the tile layout. Running the Program
To run the program, activate Simulation Mode and click on the tile Generic 1 to execute the hello world program.
If all goes well, the console window should appear.
Each time you click the tile it will add a line with the text Hello World!
Understanding the Code
Congratulations, you just ran your first Python program! But what does the code of your program really mean?
Python is an 'object oriented' language. In object oriented programming the source code is organized using 'objects'; for example, in nControl the generic tile is programmed as an object. Objects can store data and perform operations and they allow you to quickly build new thing using what you already have, rather than having to start from scratch. In this example, we wanted to print Hello World! in the console window. The generic tile has a function to do that; the print function. The print function has one argument, being the text that has to be shown in the console window. In Python, the function arguments are written between parenthesis just behind the function name. In our case the argument was a text; you specify that by putting it between quotes. So that explains the print('Hello World!') part of the code. But why did we put self. in front? When you call a function, you have to specify which object you want to use. In Python the self keyword1 specifies the object that you are currently working in. As we were working in the (code editor of the) generic tile, we can use self to refer to the generic tile. The '.' after self specifies that what follows is a property or function of that object. So in plain English self.print('Hello World!') means: ask the generic tile (= self) to process the text Hello World! with it's print function. What should you remember?
The most important thing to remember from this example is the basic structure of the code we wrote:
self.print('Hello World!') or in an abstract way: object.function(data) The three components of this statement are:
1Technically speaking self is not a reserved keyword in Python; it's a convention. You can use any name to point to the instance of the class, however, it's strongly advised to use self for that and not for anything else. nControl has been implemented using self, so in nControl you have to use self to point to the object you're working in. |