PyGTK + ipython + parasite quick tour
chrysn
2010-11-11 GTK+
Language bindings
Getting started
More GTK features The boring stuff at the beginning of a presentation
I GIMP Toolkit (created 1997 for GIMP)
I written in C
I works on X11, Windows and OS X (?)
I GTK ⊃ GObject ⊃ GLib GTK+
Language bindings
Getting started
More GTK features Python
1 import gtk 2 3 w = gtk.Window() 4 w. s h o w a l l ( ) 5 6 gtk.main() C
1 #include
1 u s i n g Gtk ; 2 3 s t a t i c i n t main(string [] args) { 4 Gtk.init(ref args); 5 6 var w=new Window(); 7 w. s h o w a l l ( ) ; 8 9 Gtk.main(); 10 11 return 0 ; 12 } Ruby
1 require ’gtk2’ 2 3 Gtk . i n i t 4 5 w = Gtk::Window.new 6 w. s h o w a l l 7 8 Gtk . main others
I C#
I Java
I JavaScript
I Perl
I R
I Lua
I Guile
I PHP
I Ada
I OCaml
I Haskell
I FreeBASIC
I D GTK+
Language bindings
Getting started
More GTK features Tools
aptitude install python-gtk2 ipython gtkparasite python-gtk2-doc Working environment
GTK MODULES=gtkparasite ipython import gtk (the Parasite window can be ignored for the moment) Getting help
1 w = gtk.Window() 2 3 w.
1 l = gtk.Label(”My label”) 2 b = gtk.Button(”My button”) 3 4 v = gtk.VBox() 5 v . p a c k s t a r t ( l ) 6 v . p a c k s t a r t ( b ) 7 8 w. add ( v ) 9 10 w. s h o w a l l ( ) 11 12 w.props.title = ”A demo program” Signals
1 def add something(widget ): 2 l.props.label +=”, and clicked” 3 4 b.connect(’clicked’, add something ) A tour through the widget library
gtk-demo pygtk-demo GTK+
Language bindings
Getting started
More GTK features UIManager definition
1
1 import gtk 2 3 w = gtk.Window() 4 5 def do new (∗ a r g s ) : 6 p r i n t ”new” 7 8 def do about (∗ a r g s ) : 9 p r i n t ” about ” UIManager usage
1 actiongroup = gtk.ActionGroup(’default ’) 2 actiongroup.add a c t i o n s ( [ 3 (’File’, None, ’File’), 4 ( ’New ’ , gtk .STOCK NEW, None, None, 5 None , do new ) , 6 (’Quit’, gtk.STOCK QUIT, None, None, 7 None, gtk.main q u i t ) , 8 (’Help’, None, ’Help’), 9 (’About’, gtk.STOCK ABOUT, None, None, 10 None , do about ) , 11 ] ) 12 13 uiman = gtk.UIManager() 14 uiman . i n s e r t a c t i o n group(actiongroup , 0) 15 uiman . a d d u i f r o m file(’uimanager.xml’) UIManager usage
1 vbox = gtk.VBox() 2 vbox . p a c k start(uiman.get widget( ’/MenuBar’), 3 expand=False) 4 vbox . p a c k start(uiman.get widget(’/ToolBar’), 5 expand=False) 6 w.add(vbox) 7 8 w. s h o w a l l ( ) 9 10 accelgroup = uiman.get a c c e l g r o u p ( ) 11 w. a d d a c c e l group(accelgroup) 12 13 w.connect(’destroy’, gtk.main q u i t ) 14 15 gtk . main ( ) GTK builder
1 import gtk 2 3 builder = gtk.Builder() 4 builder.add f r o m file(’builder.xml’) 5 6 window = builder.get object( ’MainWindow’) 7 8 window.show a l l ( ) 9 10 gtk . main ( ) Glib/GObject events
1 window.connect(’notify ::is −a c t i v e ’ , 2 t o g g l e p a u s e s t a t u s ) 3 4 import g l i b 5 6 g l i b . i d l e a d d ( r u n a b i t l a t e r ) 7 8 g l i b . i o a d d w a t c h ( s o m e file.fileno(), 9 g l i b . IO IN , r e a d m o r e d a t a ) Reference
GTK library reference http://library.gnome.org/devel/pygtk/stable/ GObject subclassing http://www.pygtk.org/articles/subclassing-gobject/ sub-classing-gobject-in-python.htm