pygobject for beginners - fedora people
TRANSCRIPT
![Page 1: PyGObject for Beginners - Fedora People](https://reader035.vdocuments.net/reader035/viewer/2022071600/613d14c9736caf36b7591690/html5/thumbnails/1.jpg)
PyGObject for Beginners
Paul W. FrieldsPresented by
Red Hat, Inc. / Fedora Project
Copyright © 2011 Paul W. Frields. This work is licensed under a Creative Commons Attribution 3.0 License.
Graduating to GUI
![Page 2: PyGObject for Beginners - Fedora People](https://reader035.vdocuments.net/reader035/viewer/2022071600/613d14c9736caf36b7591690/html5/thumbnails/2.jpg)
Today's Topics
1. Getting started
2. GObject introspection
3. Classes, inheritance, hierarchy
4. Signals
5. Putting it together: Simple example
![Page 3: PyGObject for Beginners - Fedora People](https://reader035.vdocuments.net/reader035/viewer/2022071600/613d14c9736caf36b7591690/html5/thumbnails/3.jpg)
Do you knowthe way to GTK?
![Page 4: PyGObject for Beginners - Fedora People](https://reader035.vdocuments.net/reader035/viewer/2022071600/613d14c9736caf36b7591690/html5/thumbnails/4.jpg)
ToolsPython
PyGObject >= 2.28
GTK+ >= 3.0
Text editor of choice
glade3
devhelp
![Page 5: PyGObject for Beginners - Fedora People](https://reader035.vdocuments.net/reader035/viewer/2022071600/613d14c9736caf36b7591690/html5/thumbnails/5.jpg)
Getting toolsReasonably simple on all distributions – use your distro’s package manager to easily install the proper collection
For example, on Fedora or openSUSE: Use Add/Remove Software tool to add gtk3-devel, gtk3-devel-docs, pygobject2
![Page 6: PyGObject for Beginners - Fedora People](https://reader035.vdocuments.net/reader035/viewer/2022071600/613d14c9736caf36b7591690/html5/thumbnails/6.jpg)
WorkflowGlade to design UI (as GtkBuilder)
Saved as XML file
Can be tweaked in Glade or any editor
Python code loads the XML file as a resourceInteractive elements assigned to objects
Functions called based on interaction
![Page 7: PyGObject for Beginners - Fedora People](https://reader035.vdocuments.net/reader035/viewer/2022071600/613d14c9736caf36b7591690/html5/thumbnails/7.jpg)
I still havethat other gir
![Page 8: PyGObject for Beginners - Fedora People](https://reader035.vdocuments.net/reader035/viewer/2022071600/613d14c9736caf36b7591690/html5/thumbnails/8.jpg)
Who died & left you king?Every time GTK+ changed, PyGTK had to be updated too
Using GObject introspection (GI), that’s no longer necessary
The GI repository (GIR) for a library makes it simple to generate bindings for many languages
PyGTK is the old stuff, PyGObject is the new hotness and where things are going
![Page 9: PyGObject for Beginners - Fedora People](https://reader035.vdocuments.net/reader035/viewer/2022071600/613d14c9736caf36b7591690/html5/thumbnails/9.jpg)
GObject introspection
![Page 10: PyGObject for Beginners - Fedora People](https://reader035.vdocuments.net/reader035/viewer/2022071600/613d14c9736caf36b7591690/html5/thumbnails/10.jpg)
So what?So... well, nothing really, unless you need to port existing code – not covering that here
Beginners should be aware PyGTK code on the intarwebz is in danger of becoming obsolete, or just plain wrong (gasp!)
(See earlier version of this talk for specific PyGTK guidance, URL on last page)
![Page 11: PyGObject for Beginners - Fedora People](https://reader035.vdocuments.net/reader035/viewer/2022071600/613d14c9736caf36b7591690/html5/thumbnails/11.jpg)
A Chair( ) is stilla Chair( )
![Page 12: PyGObject for Beginners - Fedora People](https://reader035.vdocuments.net/reader035/viewer/2022071600/613d14c9736caf36b7591690/html5/thumbnails/12.jpg)
GTK object modelBased on classes and inheritance
Each object can have its own special properties and methods
Real-life example: “Chair” object, has a location property
FoldingChair adds fold( ) function
SwivelChair adds rotate( ) function
![Page 13: PyGObject for Beginners - Fedora People](https://reader035.vdocuments.net/reader035/viewer/2022071600/613d14c9736caf36b7591690/html5/thumbnails/13.jpg)
GTK object hierarchyGtkButton: pushbutton widget, subclass of...
GtkBin: widget that contains only one widget, subclass of...
GtkContainer: container widget for other widgets, subclass of...
GtkWidget: base object for all widgets, subclass of...
GObject: base for all objects
![Page 14: PyGObject for Beginners - Fedora People](https://reader035.vdocuments.net/reader035/viewer/2022071600/613d14c9736caf36b7591690/html5/thumbnails/14.jpg)
How to learn moreThis is where devhelp comes in – provides hierarchical listing of inheritance
For example, let's look at GtkButton in the listing
Note that GtkButton inherits the properties and methods of the classes above (e.g. “visible” property from GtkWidget)
![Page 15: PyGObject for Beginners - Fedora People](https://reader035.vdocuments.net/reader035/viewer/2022071600/613d14c9736caf36b7591690/html5/thumbnails/15.jpg)
Just waiting for you
![Page 16: PyGObject for Beginners - Fedora People](https://reader035.vdocuments.net/reader035/viewer/2022071600/613d14c9736caf36b7591690/html5/thumbnails/16.jpg)
GTK signalsThis is the basis for interactivity and response
Main loop and input interaction
Interaction generates a signal, which can be caught and used to trigger a function
Example: a button click, a checkbox filled or cleared
![Page 17: PyGObject for Beginners - Fedora People](https://reader035.vdocuments.net/reader035/viewer/2022071600/613d14c9736caf36b7591690/html5/thumbnails/17.jpg)
Important Python pointsTo use GTK library bindings, load using GIR:from gi.repository import Gtk
Create a GtkBuilder object and use Gtk.Builder.add_from_file( ) to load objects:
def __init__(self):self.builder = Gtk.Builder()self.builder.add_from_file('prog.ui')# Now you can refer to elements using# the self.builder object
![Page 18: PyGObject for Beginners - Fedora People](https://reader035.vdocuments.net/reader035/viewer/2022071600/613d14c9736caf36b7591690/html5/thumbnails/18.jpg)
Element referencesThe Gtk.Builder.get_object( ) function returns an object based on its name
self.close_button = self.builder.get_object('close')
![Page 19: PyGObject for Beginners - Fedora People](https://reader035.vdocuments.net/reader035/viewer/2022071600/613d14c9736caf36b7591690/html5/thumbnails/19.jpg)
Connecting & callbacksAlmost any widget can use the connect( ) function to map a signal to a callback
A callback is where you code the reaction to the user’s action
self.close_button.connect(‘clicked’,self.hide_dialog)
![Page 20: PyGObject for Beginners - Fedora People](https://reader035.vdocuments.net/reader035/viewer/2022071600/613d14c9736caf36b7591690/html5/thumbnails/20.jpg)
What’s it allabout, Alfie?
![Page 21: PyGObject for Beginners - Fedora People](https://reader035.vdocuments.net/reader035/viewer/2022071600/613d14c9736caf36b7591690/html5/thumbnails/21.jpg)
Simple exampleeasy-entry.py – Takes text entry from a dialog and outputs the text on the command line
Demonstrates very simple interface and signalsWindow, buttons, text entry
Window deletion, “clicked” signal
GTK main loop
![Page 22: PyGObject for Beginners - Fedora People](https://reader035.vdocuments.net/reader035/viewer/2022071600/613d14c9736caf36b7591690/html5/thumbnails/22.jpg)
Reminder: Program flowXML file defines the GUI elements
Python code loads file into Gtk.Builder object
Python code continues to refine GUI and assign interactivity through additional functions
![Page 23: PyGObject for Beginners - Fedora People](https://reader035.vdocuments.net/reader035/viewer/2022071600/613d14c9736caf36b7591690/html5/thumbnails/23.jpg)
GTK can change the UIEverything need not be written in the UI file!
Assign label text and other content
Hide or show elements
Example: PulseCaster
![Page 24: PyGObject for Beginners - Fedora People](https://reader035.vdocuments.net/reader035/viewer/2022071600/613d14c9736caf36b7591690/html5/thumbnails/24.jpg)
(Apologies to Burt Bacharach for earlier stuff)
PyGObject doesn’t make things harder – concepts are same as in PyGTK
Try contributing to something before reinventing a wheel
If you’re doing something new, remember: Coding comes last
Parting thoughts
![Page 25: PyGObject for Beginners - Fedora People](https://reader035.vdocuments.net/reader035/viewer/2022071600/613d14c9736caf36b7591690/html5/thumbnails/25.jpg)
Copyright © 2011 Paul W. Frields. This work is licensed under a Creative Commons Attribution 3.0 License.
Questions?
http://pfrields.fedorapeople.org/presentations/