Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

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()

Cut and paste this into a Python running from the command line and a little window should pop up!

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()


Copyright © 1995-2006 [email protected]

 
 
  Published under the terms fo the GNU General Public License Design by Interspire