GNU/Linux Desktop Survival Guide by Graham Williams |
|||||
Python and GTK+ |
The simplest Python program using GTK+ is:
import pygtk pygtk.require('2.0') import gtk window = gtk.Window () window.add(gtk.Label("Hello World")) window.connect("delete-event", lambda a,b: gtk.main_quit()) window.show_all() gtk.main() |
With a little more functionality, using a callback to do something
when a button is pressed:
import pygtk pygtk.require('2.0') import gtk import random greetings = ["Goodbye Cruel World", "I'm Leaving You Today", "Goodbye, Goodbye, Goodbye"] def select_greeting (greet): return greet[random.randint(0, len(greet)-1)] def hello_button_clicked(button, label): label.set_text(select_greeting(greetings)) window = gtk.Window () vbox = gtk.VBox () button = gtk.Button("Welcome to the Machine") label = gtk.Label (choose_greeting (greetings)) window.add(vbox) vbox.add(label) vbox.pack_start(button, False, False) window.connect("delete-event", lambda a,b: gtk.main_quit()) button.connect("clicked", hello_button_clicked, label) window.show_all() gtk.main() |