Graphical Python Programming part 2: Write Your Own Screensaver - page 2
See the complete program on page 4
Colors in PyGTK, you may remember from the last article, can be specified either as color names or as Red, Green and Blue hues between 0 and 65535. You can vary your red, green and blue shades gradually, just the same way you did with the line positions. First initialize them:
r = random.randint(0, 65535) g = random.randint(0, 65535) b = random.randint(0, 65535) colorjump = 2048
Then inside the loop:
r = clip(r + random.randint(-colorjump, colorjump), 0, 65535) g = clip(g + random.randint(-colorjump, colorjump), 0, 65535) b = clip(b + random.randint(-colorjump, colorjump), 0, 65535) xgc.set_rgb_fg_color(gtk.gdk.Color(r, g, b))
If I also increase the number of steps in the loop to 500, I get something that looks like Figure 4.
Not bad! But wouldn't it be more fun if it just kept running and changing colors forever, like a screensaver?
Other Stories on LinuxPlanet
That gets a little trickier. Up to now, all the work has been done in the program's expose handler. But while the expose handler is running, the program can't be doing anything else -- like watching for clicks on the Quit button. It might not even have time to draw the screen. So you can't put a long loop, let alone one that will run forever, in handle_expose.
What's the solution? It's called an idle handler.
Solid state disks (SSDs) made a splash in consumer technology, and now the technology has its eyes on the enterprise storage market. Download this eBook to see what SSDs can do for your infrastructure and review the pros and cons of this potentially game-changing storage technology.
- 1Linux Top 3: RHEL 6.7, BackBox Linux 4.3 and RoboLinux 8.1
- 2Linux Top 3: SLES 11 SP4, Chromixium OS 1.5 and Canonical Licensing
- 3Linux Top 3: VirtualBox 5, Point Linux 3.0 and OpenSUSE Leap 42.x
- 4Linux Top 3: Linux 4.2 rc1, 4MLinux 13 and antiX15
- 5Linux Top 3: Linux Mint Rafaela, OpenMandriva Lx 2014.2 and VectorLinux 7.1