Pygobject for Beginners
Total Page:16
File Type:pdf, Size:1020Kb
Graduating to GUI PyGObject for Beginners Presented by Paul W. Frields Red Hat, Inc. / Fedora Project Copyright © 2011 Paul W. Frields. This work is licensed under a Creative Commons Attri ution !.0 "icense. Today's Topics 1. #etting started 2. #$bject introspection 3. Classes, inheritance, hierarchy 4. Signals 5. Putting it together: Simple e+ample Do you know the way to GTK? Tools Python Py#$bject ,- 2.2. #T/0 ,- 3.0 Te+t editor of choice glade! devhelp Getting tools Reasonably simple on all distributions – use your distro4s package manager to easily install the proper collection For e+ample, on Fedora or open(5(6* Use Add7Remove Software tool to add gtk3-devel, gtk3-devel-docs, pygobject2 Workflow #lade to design 58 (as GtkBuilder) Saved as <=" 1ile Can e tweaked in #lade or any editor Python code loads the <=" file as a resource 8nteractive elements assigned to o %ects Functions called based on interaction I still have that other gir Who died & left you king? Every time #T/0 changed, Py#T/ had to e updated too Using #$bject introspection 9#8), that’s no longer necessary The #8 repository 9#8R) for a library makes it simple to generate indings for many languages Py#T/ is the old stuff, Py#$ ject is the new hotness and where things are going GObject introspection So what? So... well, nothing really, unless you need to port e+isting code 3 not covering that here :eginners should e aware PyGT/ code on the intarwe > is in danger of becoming obsolete, or just plain wrong 9gasp?; 9(ee earlier version of this talk for specific Py#T/ guidance, 52" on last page; A Chair( ) is still a Chair( ) GTK object model :ased on classes and inheritance Each object can have its own special properties and methods Real-life e+ample: AChair” object, has a location property FoldingChair adds fold( ) 1unction SwivelChair adds rotate( ) 1unction GTK object hierarchy #tkButton: push utton widget, subclass o1... #tkBin* widget that contains only one widget, su class o1... #tkContainer: container widget for other widgets, subclass of... #tkWidget: ase object for all widgets& su class o1... #$bject: ase for all objects How to learn more This is where devhelp comes in – provides hierarchical listing of inheritance For e+ample, let's look at GtkButton in the listing Note that GtkButton inherits the properties and methods of the classes above (e.g. “visi le” property from GtkWidget) Just waiting for you GTK signals This is the asis for interactivity and response Main loop and input interaction 8nteraction generates a signal, which can be caught and used to trigger a 1unction 6+ample: a utton click, a checkbo+ filled or cleared Important Python points To use GT/ library indings, load using #82* from gi.repository import Gtk Create a #tkBuilder 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 Element references The Gtk.Builder.get_object( ) function returns an object based on its name self.close_button = self.builder.get_object('close') Connecting & callbacks Almost any widget can use the connect( ) function to map a signal to a callback A call ack is where you code the reaction to the user’s action self.close_button.connect(‘clicked’, self.hide_dialog) What’s it all about, Alfie? Simple example easy-entry.py – Takes te+t entry from a dialog and outputs the te+t on the command line Demonstrates very simple interface and signals Window, buttons& text entry Window deletion, AclickedB signal #TK main loop Reminder: Program flow <ML file defines the #58 elements Python code loads file into #tk.Builder object Python code continues to refine #58 and assign interactivity through additional functions GTK can change the UI Everything need not e written in the UI file? Assign la el text and other content Fide or show elements 6+ample: "ulseCaster Parting thoughts 9Apologies to :urt :acharach 1or earlier stuf1; Py#$bject doesn’t make things harder – concepts are same as in PyGT/ Try contri uting to something efore reinventing a wheel 8f you’re doing something new& remem er: Coding comes last Questions? http://pfrields.fedorapeople.org/presentations/ Contact: [email protected] Copyright © 2011 Paul W. Frields. This work is licensed under a Creative Commons Attribution 3.0 License..