Hello PyMT

Code

from pymt import *
button = MTButton(label="Hello PyMT!")
runTouchApp(button)

Discussion

The above source code is a complete PyMT application. While it does not really do anything interesting, taking a closer look can teach you a lot about how PyMT works. Let's look at it line by line:

from pymt import *

This imports the PyMT package. When this is at the top of your source code, it imports all the PyMT classes and functions so that you can use them in your program. (Normally you should only import what you need, but for demo-purposes, this is ok.)

button = MTButton(label="Hello PyMT!")

This line is probably the most interesting in this example. This code creates a variable called button and then sets the value of that variable to be an instance of the MTButton class. The expression MTButton(label="Hello PyMT!") creates a new Button with a label saying Hello PyMT!.

Note a couple of things about PyMT at this point:

  • All PyMT class names (for e.g. the types of widgets you can create) start with the letters 'MT'.
  • You can set properties of new widgets you are creating by using the prop=val syntax. In this example, we set the label of the button to say "Hello PyMT!"
runTouchApp(button)

This line actually starts your application. You can pass the widget you created and it will be in the PyMT window when it appears. Internally, calling runTouchApp will do a couple of things:

  1. It will create the window of your application.
  2. It will add the widget you pass as an argument to the window.
  3. It will start the pymt event loop. This means it will start all the input providers and make sure that your widgets are drawn every frame and receive input events by dispatching new inputs through the root widget.