Make your own sugar activities

A Standalone Python Program For Reading Etexts

The Program

Our example program is based on the first Activity I wrote, Read Etexts.  This is a program for reading free e-books.

The oldest and best source of free e-books is a website called Project Gutenberg (  They create books in plain text format, in other words the kind of file you could make if you typed a book into Notepad and hit the Enter key at the end of each line.  They have thousands of books that are out of copyright, including some of the best ever written.  Before you read further go to that website and pick out a book that interests you.  Check out the "Top 100" list to see the most popular books and authors.

The program we're going to create will read books in plain text format only.

There is a Git repository containing all the code examples in this book.  Once you have Git installed you can copy the repository to your computer with this command:

git clone git://\

The code for our standalone Python program will be found in the directory Make_Standalone_Python_gtk3 in a file named  It looks like this:

#! /usr/bin/env python
# Standalone version of
# Copyright (C) 2010  James D. Simmons
# Copyright (C) 2012  Aneesh Dogra <>
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import sys
import os
import zipfile
from gi.repository import Gtk
from gi.repository import Gdk
import getopt
from gi.repository import Pango

page = 0

class ReadEtexts():

    def keypress_cb(self, widget, event):
        "Respond when the user presses one of the arrow keys"
        keyname = Gdk.keyval_name(event.keyval)
        if keyname == 'plus':
            return True
        if keyname == 'minus':
            return True
        if keyname == 'Page_Up' :
            return True
        if keyname == 'Page_Down':
            return True
        if keyname == 'Up' or keyname == 'KP_Up' \
                or keyname == 'KP_Left':
            return True
        if keyname == 'Down' or keyname == 'KP_Down' \
                or keyname == 'KP_Right':
            return True
        return False

    def page_previous(self):
        global page
        if page < 0: page=0
        v_adjustment = self.scrolled_window.get_vadjustment()
        v_adjustment.value = v_adjustment.get_upper() - \

    def page_next(self):
        global page
        if page >= len(self.page_index): page=0
        v_adjustment = self.scrolled_window.get_vadjustment()
        v_adjustment.value = v_adjustment.get_lower()

    def font_decrease(self):
        font_size = self.font_desc.get_size() / 1024
        font_size = font_size - 1
        if font_size < 1:
            font_size = 1
        self.font_desc.set_size(font_size * 1024)

    def font_increase(self):
        font_size = self.font_desc.get_size() / 1024
        font_size = font_size + 1
        self.font_desc.set_size(font_size * 1024)

    def scroll_down(self):
        v_adjustment = self.scrolled_window.get_vadjustment()
        if v_adjustment.value == v_adjustment.get_upper() - \
        if v_adjustment.value < v_adjustment.upper - v_adjustment.page_size:
            new_value = v_adjustment.get_value() + \
            if new_value > v_adjustment.get_upper() - v_adjustment.page_size:
                new_value = v_adjustment.upper - v_adjustment.get_page_size()

    def scroll_up(self):
        v_adjustment = self.scrolled_window.get_vadjustment()
        if v_adjustment.get_value() == v_adjustment.get_lower():
        if v_adjustment.get_value() > v_adjustment.get_lower():
            new_value = v_adjustment.get_value() - \
            if new_value < v_adjustment.get_lower():
                new_value = v_adjustment.get_lower()

    def show_page(self, page_number):
        global PAGE_SIZE, current_word
        position = self.page_index[page_number]
        linecount = 0
        label_text = '\n\n\n'
        textbuffer = self.textview.get_buffer()
        while linecount < PAGE_SIZE:
            line = self.etext_file.readline()
            label_text = label_text + unicode(line, 'iso-8859-1')
            linecount = linecount + 1
        label_text = label_text + '\n\n\n'

    def save_extracted_file(self, zipfile, filename):
        "Extract the file to a temp directory for viewing"
        filebytes =
        f = open("/tmp/" + filename, 'w')

    def read_file(self, filename):
        "Read the Etext file"
        global PAGE_SIZE

        if zipfile.is_zipfile(filename):
            self.zf = zipfile.ZipFile(filename, 'r')
            self.book_files = self.zf.namelist()
            self.save_extracted_file(self.zf, self.book_files[0])
            currentFileName = "/tmp/" + self.book_files[0]
            currentFileName = filename

        self.etext_file = open(currentFileName,"r")
        self.page_index = [ 0 ]
        linecount = 0
        while self.etext_file:
            line = self.etext_file.readline()
            if not line:
            linecount = linecount + 1
            if linecount >= PAGE_SIZE:
                position = self.etext_file.tell()
                linecount = 0
        if filename.endswith(".zip"):

    def destroy_cb(self, widget, data=None):

    def main(self, file_path):
        self.window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
        self.window.connect("destroy", self.destroy_cb)
        self.window.set_title("Read Etexts")
        self.window.set_size_request(640, 480)
        self.scrolled_window = Gtk.ScrolledWindow(hadjustment=None, \
        self.textview = Gtk.TextView()
        self.textview.connect("key_press_event", self.keypress_cb)
        self.font_desc = Pango.FontDescription("sans 12")

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print "Usage: %s <file>" % (sys.argv[0])
        opts, args = getopt.getopt(sys.argv[1:], "")
    except getopt.error, msg:
        print msg
        print "This program has no options"

Running The Program

To run the program you should first make it executable.  You only need to do this once:

chmod 755

For this example I downloaded the file for Pride and Prejudice.  The program will work with either of the Plain text formats, which are either uncompressed text or a Zip file.  The zip file is named, and we can read the book by running this from a terminal:


This is what the program looks like in action:

The standalone Read Etexts program in action.

You can use the Page Up, Page Down, Up, Down, Left, and Right keys to navigate through the book and the '+' and '-' keys to adjust the font size.

How The Program Works

This program reads through the text file containing the book and divides it into pages of 45 lines each.  We need to do this because the gtk.TextView component we use for viewing the text would need a lot of memory to scroll through the whole book and that would hurt performance.  A second reason is that we want to make reading the e-book as much as possible like reading a regular book, and regular books have pages.  If a teacher assigns reading from a book she might say "read pages 35-50 for tommorow".  Finally, we want this program to remember what page you stopped reading on and bring you back to that page again when you read the book next time.  (The program we have so far doesn't do that yet).

To page through the book we use random access to read the file.  To understand what random access means to a file, consider a VHS tape and a DVD.  To get to a certain scene in a VHS tape you need to go through all the scenes that came before it, in order.  Even though you do it at high speed you still have to look at all of them to find the place you want to start watching.  This is sequential access.  On the other hand a DVD has chapter stops and possibly a chapter menu.  Using a chapter menu you can look at any scene in the movie right away, and you can skip around as you like.  This is random access, and the chapter menu is like an index.  Of course you can access the material in a DVD sequentially too.

We need random access to skip to whatever page we like, and we need an index so that we know where each page begins.  We make the index by reading the entire file one line at a time.  Every 45 lines we make a note of how many characters into the file we've gotten and store this information in a Python list.  Then we go back to the beginning of the file and display the first page.  When the program user goes to the next or previous page we figure out what the new page number will be and look in the list entry for that page.  This tells us that page starts 4,200 characters into the file.  We use seek() on the file to go to that character and then we read 45 lines starting at that point and load them into the TextView.

When you run this program notice how fast it is.  Python programs take longer to run a line of code than a compiled language would, but in this program it doesn't matter because the heavy lifting in the program is done by the TextView, which was created in a compiled language.  The Python parts don't do that much so the program doesn't spend much time running them.

Sugar uses Python a lot, not just for Activities but for the Sugar environment itself.  You may read somewhere that using so much Python is "a disaster" for performance.  Don't believe it.

There are no slow programming languages, only slow programmers.