PyGTK/Hello World with Glade and Sweettepache: Difference between revisions

From OLPC
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
PyGTK/Hello World with Glade and Sweettepache
PyGTK/Hello World with Glade and Sweettepache


The [[PyGTK/Hello World Tutorial]] shows how to create the python program Hello World as a Sugar Activity. [[pyGTK/Hello World with Glade]]
The [[PyGTK/Hello World Tutorial]] shows how to create the python program Hello World as a Sugar Activity. [[PyGTK/Hello World with Glade]]
shows how to build the same activity using Glade. <i>Glade.py</i> provides a visual editor for designing a program's GUI (graphical user interface). The output from Glade is a file '<i>name.glade</i>' where name is the name of the activity.
shows how to build the same activity using Glade. <i>Glade.py</i> provides a visual editor for designing a program's GUI (graphical user interface). The output from Glade is a file '<i>name.glade</i>' where name is the name of the activity.


Another python program, <i>tepache.py</i>, processes the glade file to produce a skeleton module <i>name.py</i> which has the necessary code to connect with the GUI. Only the specific application logic needs to be added to complete the module.
Another python program tepache, <i>tepache.py</i>, processes the glade file to produce a skeleton module <i>name.py</i> which has the necessary code to connect with the GUI. Only the specific application logic needs to be added to complete the module.


The program sweettepache.py is a modification of tepache.py which produces the name.py module and, in addition, produces a nameActivity.py module for Sugar, and produces the files and folders needed to make an activity bundle. The result is a module name.py which can be tested on Ubuntu(or possibly other platforms). When the module works correctly on Ubuntu, it and the activity structure can be ported unchanged to the Xo.
The program <i>sweettepache.py</i> is a modification of this program which, in addition to <i>name.py</i>, produces a <i>nameActivity.py</i> module for Sugar, and produces the files and folders needed to make an activity bundle. The result is a module <i>name.py</i> which can be tested on Ubuntu (or possibly other platform). When the module works correctly on Ubuntu, it and the activity structure can be ported unchanged to the XO.


The purpose of this page is to make sweettepache.py available and to show how it can be used in rapidly building and testing sugar-compatible python code.
The purpose of this page is to make sweettepache.py available and to show how it can be used to build and test sugar-compatible python code.


This project was developed on Ubuntu 7.10 and the Hello World activity was ported and tested on a G1G1 Xo upgraded to 656. An important caveat is that there has been no robust testing (after all, almost anything can work for 'Hello World').
This project was developed on Ubuntu 7.10 and the Hello World activity was ported and tested on a G1G1 XO upgraded to release 656. An important caveat is that there has been no robust testing (after all, almost anything can work for 'Hello World').


==Activity Devlopment Process==
==Activity Devlopment Process==

Revision as of 19:31, 20 February 2008

PyGTK/Hello World with Glade and Sweettepache

The PyGTK/Hello World Tutorial shows how to create the python program Hello World as a Sugar Activity. PyGTK/Hello World with Glade shows how to build the same activity using Glade. Glade.py provides a visual editor for designing a program's GUI (graphical user interface). The output from Glade is a file 'name.glade' where name is the name of the activity.

Another python program tepache, tepache.py, processes the glade file to produce a skeleton module name.py which has the necessary code to connect with the GUI. Only the specific application logic needs to be added to complete the module.

The program sweettepache.py is a modification of this program which, in addition to name.py, produces a nameActivity.py module for Sugar, and produces the files and folders needed to make an activity bundle. The result is a module name.py which can be tested on Ubuntu (or possibly other platform). When the module works correctly on Ubuntu, it and the activity structure can be ported unchanged to the XO.

The purpose of this page is to make sweettepache.py available and to show how it can be used to build and test sugar-compatible python code.

This project was developed on Ubuntu 7.10 and the Hello World activity was ported and tested on a G1G1 XO upgraded to release 656. An important caveat is that there has been no robust testing (after all, almost anything can work for 'Hello World').

Activity Devlopment Process

The following is the step by step process in building and testing a Sugar Activity using Sweettepache.

Design the GUI

See PyGTK/Hello World with Glade for a description of this step. Make a folder, name.activity. Save the glade file in that folder as name.glade The important point is that when the glade file is saved, its name (name.glade) will be used by sweettepache as the name of the activity. Also, the folder name.activity will be the top-level of the Sugar bundle.

Create the skeleton module

Copy sweettepache.py to name.activity Open a Terminal and 'cd' to the folder name.activity Execute the command: python sweettepache.py name.glade

Instantly you have an activity bundle complete with icon! In addition to the flags which come with tepache.py (see ...), the -s flag makes sweettepache work identically to tepache (no sweetening). The flag -i flag requests output of a generic icon (.svg).

Only in demos do programs work correctly the first time. In reality, you will want to change the GUI. To do this, open name.glade in Glade, make changes, and save the modified file. Run sweettepache again. Sweettepache saves a copy of your module as name.py.bak. It also creates a copy of the module as name.py.orig (the only difference is that sweettepache 'normalized the indents' in name.py.orig). Sweettepache creates a temporary diff file with the new skeleton to detect your application code, and then produces a new name.py which incorporates your code with the modified interface to the GUI.

Test on Ubuntu

It is probably a waste of time to test a program on the Xo as an Activity if it doesn't work correctly on Ubuntu. Sweettepache produces a module nameActivity.py which incorporates the special code required on the Xo. The intention is that name.py runs on Ubuntu as is. On the Xo, nameActivity.py initializes the Activity and then calls name.py. You may need to add to nameActivity.py to change the toolbar, for example. Currently sweettepache will write over the nameActivity.py. You can either save a copy of your modified version before rerunning sweettepache.

Test on the Xo

Currently I use a USB stick. Simply copy the name.activity folder to the stick. On the Xo, open a Terminal. Change to root (su). Copy the name.activity folder from the stick to /home/olpc/Activities. Restart Sugar (Ctl + Alt + Erase). Your activity should show up on the bottom panel. Start your activity.

If, after a reasonable time it is still 'starting', it usually means that python found a syntax or other error that prevented the program from starting. Often the explanation will be found in the activiity log. Open the log viewer (this shows the logs stored in /home/olpc/.sugar/default/logs). There should be an entry for your activity. This will show messages from python as well as the output of print statements in your code.

There are two (probably temporary) gotchas. When you open a Terminal on the Xo, the focus is on the toolbar, not the prompt. When the Terminal opens, click the mouse in the Terminal window first. The second is that there seems to be no way to 'stop' an activity that is 'starting'. It requires the CTL-ALT-Erase solution.

If the problem which prevents the activity from executing is simple, it can be fixed in the Terminal. As root (su), nano is available to edit your module in /home/olpc/Activities/name.activity.

Once the program starts, you will immediately see one of the main differences between Ubuntu and Sugar, the GUI window opens full screen on Sugar. This may well require tweaking of the Glade design to make the results more attractive on the Xo.

Technical details

Normally, glade generates the root window for an application. However, Sugar provides the toplevel window. The result is that the GUI definition from glade must be connected to the sugar window. This means that the glade file is processed on sugar beginning at the top widget below the level of the window (for example, a vbox container). This widget is then packed into an existing vbox container supplied by sugar. The details of this are apparently changing from release to release. The first tutorial (PyGTK/Hello World) didn't use Glade. The page PyGTK/Hello World with Glade explains the issue clearly. However, now that solution doesn't work because of the pre-existing vbox in the sugar window. There are, of course, no guarantees that a later release of Sugar will not break sweettepaches solution.

One additional advantage of the sweettepache strategy is that all of the details of building an activity structure can be encapsulated in a form which is easily changed. The last half of sweettepache is the 'boilerplate' code it generates. These lines are customized for a specific application by entries of the form %(class)s which produces a variable output depending on the contents of 'self.data'. The entry %(t)s produces a tab or four spaces (as needed by python). Caution: the trailing s is needed (it means produce a string).

This scheme makes it easy to modify the copy of sweettepache in the activity folder to generate specific code such as an additional button on the taskbar.

The module 'hello.py' produced by sweettepache

The module 'helloActivity.py' produced by sweettepache

The sweettepache program source

Changes made to tepache to produce sweettepache

References