Pygtk beyond saying hello world


Some time ago, I found a folder full of pygtk examples somewhere and copied it to /home. I think it comes with pygtk itself but I’m too lazy to check now. Anyway, they were very interesting examples. The problem I find with learning programming is that the examples tend to be either too easy or too hard to comprehend. Or maybe I’m just too lazy to learn what is too hard.

Anyway anyway anyway… the one example that stood out to me the most is called, which shows how to display text in windows in the GtkTextView widget. The example tries to show off just about all of the widget’s features, which makes it rather impractical for learning when there’s stuff everywhere. So I tried my best to take it apart, to learn which bit did what.

I was thinking, it would be a good exercise to make a simple text editor. This would be the first step to that.

The product of a couple of hours of frustration is this.

#! /usr/bin/env python

import pygtk
import os
import gtk
import pango

class TextViewer:
    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_title("A text viewer")
        self.window.set_default_size(400, 400)
        view = gtk.TextView()
        buffer = view.get_buffer()
        sw = gtk.ScrolledWindow()
        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
    def create_tags(self, text_buffer):
        text_buffer.create_tag("mainbody", family="monospace")
    def insert_text(self, text_buffer):
        iter = text_buffer.get_iter_at_offset(10)
        text_buffer.insert_with_tags_by_name(iter, "This is supposed to be a text editor!", "mainbody")

def main():

if __name__ == '__main__':

All it does is create a 400×400 window with the words “This is supposed to be a text editor!”. Pretty basic, but hey, I’ve got to start somewhere. I would include a screenshot but there isn’t much to see, plus I am using dwm which means it really does create a white square on the screen, and nothing else. I’m surprised I even managed to navigate through the code-jungle of tags and buffers and functions. From what I surmised, the text buffer holds the text, which you have to attach tags to, which are like styles you have in word processors (bold, italic, 14 point font, and so on). If this is supposed to be easy then I don’t really want to contemplate repeating the same feat in C.

While hacking that, I found a really nice post of someone who made a text entry dialog in Pygtk. The code doesn’t look too difficult to understand either.

And since I’m already blabbering on about Pygtk: EveryGUI looks like an impressive project. Building a more friendly graphical front end for terminal commands is neat. Unfortunately I don’t think I need it for myself; this laptop is so monstrously slow even with executing my faux text editor code above.

I should also find out more about getting syntax highlighting to work. I got the code above to work by copying the pre tags from the text entry dialog blog entry, but it doesn’t give the same effect. Oh well, at least the indenting is correct.

So, in summary, Pygtk = fun. šŸ™‚

%d bloggers like this: