And on the 7th day there were PyGTK custom icons…

By on January 18th, 2010 in Python Leave a Comment

It’s been a few days since our first bumbling foray into custom PyGTK icons using PyCairo, and now it’s time to reap the benefits. We’re concocting a few simple classes which will make drawing and icon creation a breeze. Your final, glorious product should look like this:

Unimpressive, you say? Well maybe so, but take a look at the code which created it:

import gtk
from screwdriver.window import Window
from screwdriver.image import DrawnImage
from screwdriver.toolbar import ToolbarButton, Toolbar
import bazooka.filters as filters
 
circle = DrawnImage()
circle.draw_circle(x=5, y=5, radius=20)
 
rect = DrawnImage()
rect.draw_rectangle(x=5, y=5, width=40, height=40)
 
line = DrawnImage()
line.draw_line(x=5, y=5, length=50)
 
toolbar = Toolbar(tool_items=[ToolbarButton(icon=circle, label='Ellipse'), ToolbarButton(icon=rect, label='Rectangle'),
                              ToolbarButton(icon=line, label='Line')])
toolbar.set_size_request(260, 100)
 
window = Window(child=toolbar)
gtk.main()

The goal here was human-readable, consolidated, flexible Python code. Simple circles are not the end of the game: the Screwdriver DrawnImage class makes use of Bazooka’s graphics module, so rounded rectangles, ellipses, and gradient fills are available (warning, this code is in the alpha stage). Background: Screwdriver is a Python framework sitting on top of PyGTK, meant to simplify working with PyGTK, and Bazooka is a Python UI framework built on top of PyClutter, a drawing framework which utilizes OpenGL under the hood. All of the source code for Bazooka and Screwdriver, including the source code and classes displayed above, can be found on our Screwdriver, Bazooka and Machete repositories which will be up on Git Hub imminently.