Download presentation
Presentation is loading. Please wait.
1
Fedora Project / Red Hat
PyGTK for Beginners Paul W. Frields Fedora Project / Red Hat Southeast LinuxFest June 13, 2010 Very excited to be here Thank the entire FOSSMeet team for their help in setting up this talk The Fedora Project is proud to help sponsor this conference, spreading FOSS A little about me: * Worked with Linux for >10 years * Joined Fedora Project in 2003 as a volunteer * Inaugural Board member in 2006 * February – Red Hat full time as FPL Copyright © 2010 Paul W. Frields, some rights reserved. Licensed under a Creative Commons Attribution-ShareAlike (CC BY-SA) 3.0 license.
2
What this talk covers Tools you can use to build and understand a graphical user interface (GUI) Understanding the GTK object and signal models How to hook Python code to a GUI Things to consider when designing your application (code and GUI)
3
What this talk does not cover
How to be a good programmer The Python language Intro for programmers: Mark Pilgrim's Dive Into Python For absolute beginners: Wesley Chun's Core Python Programming User experience, human-computer interaction
4
What this talk presumes
You know elementary programming concepts (variables, conditionals, loops, functions) You've written Python or another scripting language but you're not a programmer You are using a Linux system, not necessarily Fedora
5
Things you need Python GTK Text editor of choice
6
Things you'll want Glade3 Devhelp
7
Installing the tools Fedora, openSUSE: Use Add/Remove Software tool to add the GNOME Software Development collection Ubuntu: Use Synaptic Package Manager tool to add the python-gtk2-dev and glade packages
8
Workflow Glade to design UI (as GtkBuilder) Saved as XML
Can be tweaked in Glade or any editor Python code loads the XML file as a resource Interactive elements assigned to objects Functions called based on interaction
9
GTK object model Based 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
10
GTK object hierarchy example
GtkButton: pushbutton widget, subclass of... GtkBin: widget that contains only one widget, subclass of... GtkContainer: widget that's a container for other widgets, subclass of... GtkWidget: object that is the base for all widgets, subclass of... GtkObject: base for all objects
11
How to learn more This 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)
12
GTK signals This 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
13
Important Python points
import gtk lets you use GTK library bindings Create a GtkBuilder object Use gtk.Builder.add_from_file( ) to load objects def __init__(self): self.builder = gtk.Builder() self.builder.add_from_file('prog.builder') # Now you can refer to elements using # the self.builder object
14
Element references The gtk.Builder.get_object( ) function returns an object based on its name self.close_button = \ self.builder.get_object('close')
15
Simple example easy-entry.py – Takes text entry from a dialog and outputs the text on the command line Demonstrates very simple interface and signals Window, buttons, text entry Window deletion, “clicked” signal GTK main loop
16
Two approaches Write code, add GUI Design GUI, write code to fit
17
Code first? For GUI wrappers around an existing program
Not all options are worth GUI-izing! GConf keys (soon GSettings) can store some tweakable configuration
18
Design first? Know your user and use cases Who is this tool for?
How will they use it? How will it integrate with other apps? Draw a mockup first (paper is fine)
19
Using Glade for mockups
Use boxes liberally GNOME Human Interface Guidelines provide layout help for a matching interface (Section 8, “Visual Design”) Many standard dialogs are easy to create
20
Intermediate topics
21
Distributing your work
Write a setup.py file The setup( ) function takes parameters such as simple sequences that let distutils do the work MANIFEST.in file to include source content Simple commands for builds, tarballs python setup.py --help-commands
22
Reminder: Program flow
XML 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
23
PyGTK can change the UI Not everything has to be written in the Glade file! Assign label text and other content Hide or show elements Example: PulseCaster
24
I18n and L10n I18n (internationalization) is the process of making code multilingual L10n (localization) is the process of translating content Don't restrict your audience to just English speakers
25
Adding i18n Transfer labels to your Python code
Add gettext function import gettext _ = lambda x: gettext.ldgettext('myprog', x) Mark all strings with the _( ) function But now what? How to get them into translatable formats?
26
Babel to the rescue! Install the python-babel package
Wraps around Python distutils to provide message (text string) extraction, for instance based on the _( ) function: python setup.py extract_messages
27
Thank you! Questions? pfrields@fedoraproject.org
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.