Sunday, April 07, 2013

Simple computer program for one year olds

Kids love to use the same things that their parents are using. Rather than buying them toy versions of objects, we use these opportunities to teach them the correct way to use everyday objects.

My son loves to see us use the computer, and wants to use a computer too. So we bought a real adult keyboard at an electronic store, and attached it to a real computer running Linux. The only thing he can do is hit a key and see the computer's reaction.  In most programs, this produces no visible result.

So I wrote a very simple program in Python using Gtk to display a single character from the keyboard in a huge font. This is what the computer looks like when it is running this program:


Lots of advantages to this approach:
  1. The child uses a "real" keyboard and a real computer, something that parents use too.
  2. There is no way to exit, except to Alt-Tab to switch programs.
  3. The child can be rough with the spare keyboard, you just buy a new one.
  4. The computer can be disconnected from the Internet, and put on a spare child account if required.
  5. The program should work on Windows and Mac as well, after you set up Python + Gtk.
The entire program is listed here:
#!/usr/bin/python

import gtk, string, pango

class BigChar():
    """ Create a Gtk window for a single giant textview that accepts all keyboard input. """
    def on_key_press(self, widget, data=None):
        """ Intercept all keypress events and show ascii
            characters. This requires the CAPS_LOCK to be off.  We
            don't intercept CAPS NUM or SCROLL lock, probably
            should."""
        ascii_value = data.keyval
        if (ascii_value >= 97 and ascii_value <= 122):
            self.textBuffer.set_text(string.ascii_uppercase[ascii_value-97])
            start = self.textBuffer.get_start_iter()
            end = self.textBuffer.get_end_iter()
            self.textBuffer.apply_tag_by_name("real_big", start, end)

    def __init__(self):
        """ Create a window with a single giant text view. Disables all chrome.
        """
        self.w = gtk.Window(gtk.WINDOW_TOPLEVEL)
        # No border
        self.w.set_border_width(0)
        self.w.realize()
        # Take over the entire screen
        self.w.fullscreen()

        # Connect the callback on_key_press to the signal key_press.
        self.w.connect("key_press_event", self.on_key_press)
        # Make the widget aware of the signal to catch.
        self.w.set_events(gtk.gdk.KEY_PRESS_MASK)

        # Add a text view to show the key pressed
        self.textView = gtk.TextView()
        # Disable a cursor in the text view.
        self.textView.set_editable(False)
        self.textView.set_can_focus(False)
        # Show the single character in the middle
        self.textView.set_justification(gtk.JUSTIFY_CENTER)
        # This is the place we will write the character to
        self.textBuffer = self.textView.get_buffer()
        # Make the text view huge, blue on white
        fontdesc = pango.FontDescription("monospace 512")
        self.textView.modify_font(fontdesc)
        tag = self.textBuffer.create_tag("real_big", background="white", foreground="red")

        # Make the text view take the entire window
        self.hbox = gtk.HBox(homogeneous=False, spacing=0)
        self.hbox.pack_start(self.textView, expand=True, fill=True)
        self.w.add(self.hbox)

    def show(self):
        """ Show the window"""
        self.w.show_all()


if __name__ == '__main__':
    # Create a bigchar window, and show it.
    bigchar = BigChar()
    bigchar.show()
    gtk.main()