TITLE OF PAPER: GTK and Gnome programming with Python URL OF PRESENTATION: _URL_of_powerpoint_presentation_ PRESENTED BY: Travis B. Hartwell REPRESENTING: _author_affiliation_here_ CONFERENCE: PyCON 2004 DATE: 20040325 LOCATION: Amphitheater -------------------------------------------------------------------------- REAL-TIME NOTES / ANNOTATIONS OF THE PAPER: {If you've contributed, add your name, e-mail & URL at the bottom} PyGtk and PyGnome programming GUI Programming Sometimes can be not very fun Is necessary to make useful and productive applications The combination makes GUI programming relatively painless and quick Our Example: PyCon Chater A chat client -- "real world" yet simple enough to demonstrate basic GTK and Gnome application Stage 1 - Plain Gtk API Using plain Gtk API Calls - very tedious to do Basic app screen being show with a File menu and Edit menu and a text display with text input box Containers Many widgets in Gtk are Container widgets Used to manage layouts of other widgets, such as: Buttons text entry etc Other widgets are added or packed to these containers Containers: Example win = gtk.Window() win.add(gtk.button("Ok") win.show_all() gtk.main() Signals and Event Handlers Widgets in Gtk emit signals when events ahppen (button click, etc.). To handle thie signal, you must connect to a Python method Event Handlers: First connect the signal: self.entry.connect("activate", self.sendMessage) the define the handler def sendMessage(self, entry): self.buffer.insert_at_cursor() self.entry.set_text('') Stage 2: Glade GUI code for widget creation and layout can become edious Glade is a user interface builder for Gtk Originally, glade would geneate C or C++ code libglade loads the glade files at runtime Now the preferred method of using glade the only way to use glad with Python Gives you the typical performance and code size efficiencies by putting the drudge work of UI loading into the libglade library and you can just refer to the xml glade files Glade Demo and libglade usage After you have created and saved your glade layout, you load it like this: self.xml = gtk.glade.XML('chat-w.glade') self.entry = self.xml.get_widget('entry') signals = {'sendMessage': self.sendMessage} self.xml.signal_autoconnect(signals) things are now much simpler! Plain Gtk API call version: 77 lines functinally equivalent Glade version: 37 lines this difference will only get larger with more complex GUIs Glade also allows you to make changes to he layout and widget properties without touching source code Stage 3 - Dialog boxes and external events Pycon Chattter isn't useful without netwokr connectivity dialog and message boxes Message boxes Gtk provides a message box containing a small image used for describing the message type and then a small message For example, to create and display a error dialog: error = gtk.MessageDialog(self.window, gtk.DIALOG_MODAL | more options gtk.MESSAGE_ERROR ) Q: does error block? A: he thinks it does - doesn't know if it passes it off or waits Demo! modal dialogs work. dialog boxes Dialog boxes are instance of gtk.dialog buttons and other widgets in a dialog box emit "responses" To wait for user response call myDlg.run() A dialog box is used for the Connect Dialog Multiple windows in a Glade file Since all windows and dialog boxes for my application are defined in one glade file: Specify the window or dialog name when loading self.xml = gtk.glade.XML('chat-3.glade', 'window') Each window or dialog is in its own class Receiving external events PyGTK programs can also resond to external non-GUI events (Useful for network apps) The Twisted Protocol Factor calls the writeMessage method on our Chatter class: def lineReceived (self, line): self.chatter.writeMessage(line) def writeMessage(self, msg): self.buffer.insert_at_cursor('%s\n' % msg) For the curiously Twisted the GTK and Twisted main loops need to be integrated Beofore either gtk or the twisted reactgors are import: reactor.run() vs gtk.run() Demo! showed example of using gconf to store configuation values Storing configuration data with GConf. Essentially a prefs database with handy hooks: self.gconfClient = gconf.client_get_default() self.username = self.gconfClient.get_string("/path/to/location") self.gconfClient.set_string("/path/to/location", value) Just for fun: Gnome Panel Applet there are also bindings for writing Gnome panel applets An applet factory server files Applet Factory: Gnome uses a factory to create you applet instance def factory(container, iid): applet = Applet(container) container.show_all() return gtk.TRUE You then call the Gnome bonobo factory methods gnome.applet.bonobo_factor("OAFIID:GNOME_ChatApplet_Factory", gnome.applet.Applet.__gtype__, "ChatApplet", "0", factory) Server Files: Prepare to be scared showing a huge ugly file that has to be registered with the bonobo activation server -- looks like some sort of IDL magic (but in XML!) :(. Change name, icon, etc. this file needs to o in your Bonobo Activation path: /usr/lib/bonobo/server a directory pointed to for your gnome setup Again, for the curiousely twisted in addition to the gtk2reactor import as before, Instead: reactor.startRunning() reactor.simulate() Conclusions Python along with PyGTK and PyGnome is a very viable platform for application development Coupled with Glade and the Python interactive interpreter, it truly can be for what some call "Rapid Application Development" Allows for rapid development when using the strength of python. learning the gui and layout design is much easier when you can use the power of the interactive prompt in python Q: ever used wxPython A: he has used it previously but he uses pyGtk because he primarily uses Gnome gtk seemed to fit better when porting Q: Does the gtk play well with the Twisted sync model? A: he hasn't had any problem with gconf -- you can watch for changes within the DB -- because the main loop is integrated it handles the updating just fine Q: there are different versions of Glade available, are you using Glade 2? A: there are two incompatible versions - 1.2 and improved version is 2.x series - the api's are different - glade 2 is for 2.x series - glade 3 is in the works but timeline is unknown to him Q: do you have any experiance porting gtk to windows? A: I havent done a lot with that, i know GTK runs well, used things such as XChat. there are python apps using Gtk, Gtkwin(a native looking wrapper). Not really a way that has everything packaged together. audience comment: if you stick to pygtk and don't use anything that is gnome specific you are ok Q: how do you actually see the widgits appear using the interactive interpreter? A: on ActiveState in pycookbook, code to actively create windows & goo. -------------------------------------------------------------------------- REFERENCES: {as documents / sites are referenced add them below} http://www.travishartwell.net http://www.pygtk.org http://http://glade.gnome.org/ -------------------------------------------------------------------------- QUOTES: "Emacs & Glade are enough IDE for me." -------------------------------------------------------------------------- CONTRIBUTORS: {add your name, e-mail address and URL below} Bob Kuehne, rpk@blue-newt.com, http://www.blue-newt.com Mike Taylor, bear@code-bear.com Linden Wright, lwright@mac.com -------------------------------------------------------------------------- E-MAIL BOUNCEBACK: {add your e-mail address separated by commas } -------------------------------------------------------------------------- NOTES ON / KEY TO THIS TEMPLATE: A headline (like a field in a database) will be CAPITALISED This differentiates from the text that follows A variable that you can change will be surrounded by _underscores_ Spaces in variables are also replaced with under_scores This allows people to select the whole variable with a simple double-click A tool-tip is lower case and surrounded by {curly brackets / parentheses} These supply helpful contextual information. -------------------------------------------------------------------------- Copyright shared between all the participants unless otherwise stated...