Jump to content

User:Bhanutpt/scratchpad/python/pyGTK

From Wikibooks, open books for an open world

GTK is part of GNOME project and is used to create advanced graphical user interfaces, pyGTK is the wrapper of the GTK library on python. This book aims at learning by constructive examples.

Examples

[edit | edit source]

The basic GUI program to pop up a simple window

#!/usr/bin/env python
import gtk
win = gtk.Window()
win.show()
gtk.main()  # not win.main()

Its always good to use class paradigm in programming GUI using pyGTK

#!/usr/bin/env python
import gtk
class StartWindow:
    def __init__(self):
        win = gtk.Window()
        win.show()
StartWindow()    
gtk.main()

The window quit method is not proper in the program, the intercommunication and integrity of the widgets in pyGTK are based on events and signals, so when the user actually clicks on the 'X' button, it generates a signal called "destroy", if there is an event to handle the signal, it will be triggered, gtk.main_quit() is the right method that will quit the GUI window in its proper method. So use win.connect("destroy", callback) to connect the "destroy" signal generated by the window manager and the respective callback function; in callback function call the method gtk.main_quit()

#!/usr/bin/env python
import gtk
class StartWindow:
    def __init__(self):
        win = gtk.Window()
        win.connect("destroy", self.close)
        win.show()
    def close(self, widget):    #add the variable widget as the caller calls 
        gtk.main_quit()         #with two arguments
StartWindow()    
gtk.main()

Now let us add a button widget to the window and link the "clicked" signal from the window manager with the call back function print_message, where it prints a message to console

#!/usr/bin/env python
import gtk
class StartWindow:
    def __init__(self):
        win = gtk.Window()
        win.set_title("Simple Window")
        #win.set_border_width(10)       #uncomment and see the change      
        #win.set_size_request(200,50)   #uncomment and see the change
        win.connect("destroy", self.close)
        bu = gtk.Button("start")
        bu.connect("clicked", self.print_message)    
        win.add(bu)             #don't forget to add button to window
        win.show_all()          #use show_all to display all widgets
    def close(self, widget):    
        gtk.main_quit()         
    def print_message(self, widget):
        print "clicked..."
StartWindow()    
gtk.main()

Now, we have to see ways to accommodate more buttons in the desired pattern into the window, to be more generic, we have to know how to pack widgets into the window, here is a little example with two buttons, a separator and a quit button to quit the window

#!/usr/bin/env python
import gtk
class StartWindow:
    def __init__(self):
        self.win = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.win.set_title("Allign Buttons")
        self.win.connect("destroy", self.close)
        self.win.set_border_width(10)
        self.win.set_size_request(225, 144)

        verticalBox = gtk.VBox(True, 0)                         #homogeneous, spacing

        la = gtk.Label("Label")
        la.set_alignment(0.5, 0)
        verticalBox.pack_start(la, False, False, 0)             #child, expand, fill, padding
        la.show()

        horizontalBox = gtk.HBox(True, 0)                       #homogeneous, spacing
        bu = gtk.Button("start")
        bu.connect("clicked", self.print_start)
        horizontalBox.pack_start(bu, True, True, 0)             #child, expand, fill, padding
        bu.show()
        bu = gtk.Button("stop")                                 #note that the samle handler bu is used twice
        bu.connect("clicked", self.print_stop)
        horizontalBox.pack_start(bu, True, True, 0)             #child, expand, fill, padding
        bu.show()
        verticalBox.pack_start(horizontalBox, True, True, 0)    #child, expand, fill, padding
        horizontalBox.show()

        sep = gtk.HSeparator()
        verticalBox.pack_start(sep, True, True, 0)              #child, expand, fill, padding
        sep.show()

        hbx = gtk.HBox(True, 0)                                 #homogeneous, spacing
        qb =  gtk.Button("quit")
        qb.connect("clicked", lambda w: gtk.main_quit())  
        hbx.pack_start(qb, False, False, 0)                     #child, expand, fill, padding
        verticalBox.pack_start(hbx, False, False, 0)            #child, expand, fill, padding
        qb.show()
        self.win.add(verticalBox)
        verticalBox.show()
        self.win.show_all()          

    def close(self, widget):                             #add the var widget as the caller calls 
        gtk.main_quit()                                  #with two arguments
    def print_start(self, widget):
        print "started something..."
    def print_stop(self, widget):
        print "stopped something..."
StartWindow()    
gtk.main()