TM
GNOME application development Summary
A beginner’s guide to GNOME 3 applicationdevelopment
David King <[email protected]>
23rd May 2014 / GNOME.Asia / #8 conference roomLicensed under CC0-1.0
TM
GNOME application development Summary
Overview
GNOME as a platform forapplication development6-month developmentcyclewrite applications inPython, JavaScript, Vala orC
TM
GNOME application development Summary
Platform libraries
GLib (including GObjectand GIO)GTK+many others (see theapplication developmentoverview ondeveloper.gnome.org)
TM
GNOME application development Summary
Development tools
Glade, for designing GUIsDevhelp, for browsing API referencedocumentationAnjuta, an integrated developmentenvironment (IDE)Builder, a future GNOME IDEInspector, a GTK+ tool for debuggingUIs
TM
GNOME application development Summary
Before we start
Clone the git repository: git clonegit://fedorapeople.org/home/fedora/amigadave/public_git/python-gnome-app.git
Browse through the git commits:http://fedorapeople.org/cgit/amigadave/public_git/python-gnome-app.git/
Open your favourite text editor or IDETry running the application: ./python-gnome-app
TM
GNOME application development Summary
Hello world!
Import GObject-IntrospectionShow a windowRun the GTK+ main loopThe application must be killed externally!
TM
GNOME application development Summary
Hello world code
# ! / usr / b in / python3
from g i . r e p o s i t o r y import Gtk
window = Gtk . Window ( )window . show_al l ( )
Gtk . main ( )
TM
GNOME application development Summary
Signals and handlers
Subclass (inherit) GtkApplicationConnect the activate signal of the application to ahandlerCreate or show the window in the handlerGTK+ widgets (and other GObjects) have signals, whichare documented in the API referencesThe application terminates when the window is closedSee https://wiki.gnome.org/HowDoI/GtkApplication formore details on GtkApplication
Signals and handlers code 1
# ! / usr / b in / python3
import sysfrom g i . r e p o s i t o r y import Gtk
class PythonApp ( Gtk . A p p l i c a t i o n ) :
def _ _ i n i t _ _ ( s e l f ) :Gtk . A p p l i c a t i o n . _ _ i n i t _ _ (
s e l f ,a p p l i c a t i o n _ i d =" org . example . PythonGnome " )
s e l f . connect ( " a c t i v a t e " , s e l f . on_ac t i va te )
def on_ac t i va te ( s e l f , app ) :window = Gtk . Appl icat ionWindow ( a p p l i c a t i o n =app )window . show_al l ( )
# Continued on next s l i d e .
TM
GNOME application development Summary
Signals and handlers code 2
# Continued from prev ious s l i d e .
app = PythonApp ( )e x i t _ s t a t u s = app . run (None )sys . e x i t ( e x i t _ s t a t u s )
TM
GNOME application development Summary
Keybindings and actions
Add an action for quitting the application, and another forprinting hello worldConnect the activate signal of the actions to handlersAdd an accelerator to each actionSee https://wiki.gnome.org/HowDoI/GAction for moredetails
Keybindings and actions code 1
# ! / usr / b in / python3
import sysfrom g i . r e p o s i t o r y import Gio , Gtk
class PythonApp ( Gtk . A p p l i c a t i o n ) :
def _ _ i n i t _ _ ( s e l f ) :Gtk . A p p l i c a t i o n . _ _ i n i t _ _ (
s e l f , a p p l i c a t i o n _ i d =" org . example . PythonGnome " )s e l f . connect ( " a c t i v a t e " , s e l f . on_ac t i va te )s e l f . connect ( " s t a r t u p " , s e l f . on_star tup )
def on_star tup ( s e l f , app ) :s e l f . window = Gtk . Appl icat ionWindow ( a p p l i c a t i o n =app )he l lo_wor ld = Gio . SimpleAct ion (
name=" he l lo−world " , parameter_type=None )s e l f . add_act ion ( he l l o_wor ld )
# Continued on next s l i d e .
Keybindings and actions code 2
# Continued from prev ious s l i d e .s e l f . add_acce lera tor (
"<Primary >h " , " app . he l lo−world " , None )he l lo_wor ld . connect ( " a c t i v a t e " , s e l f . on_hel lo_wor ld )
q u i t = Gio . SimpleAct ion (name=" q u i t " , parameter_type=None )
s e l f . add_act ion ( q u i t )s e l f . add_acce lera tor ( "<Primary >q " , " app . q u i t " , None )q u i t . connect ( " a c t i v a t e " , s e l f . on_qui t )
def on_ac t i va te ( s e l f , app ) :s e l f . window . show_al l ( )
def on_hel lo_wor ld ( s e l f , ac t i on =None , param=None ) :pr in t ( " He l lo wor ld ! " )
def on_qui t ( s e l f , ac t i on =None , param=None ) :s e l f . q u i t ( )
# Continued on next s l i d e .
Keybindings and actions code 3
# Continued from prev ious s l i d e .
app = PythonApp ( )e x i t _ s t a t u s = app . run (None )sys . e x i t ( e x i t _ s t a t u s )
TM
GNOME application development Summary
Application menus
Create a menu modelLink the menu items to actions, in the correct groupSet the application menu on the applicationSee https://wiki.gnome.org/HowDoI/ApplicationMenu formore details
Application menus code 1
# ! / usr / b in / python3
import sysfrom g i . r e p o s i t o r y import Gio , Gtk
class PythonApp ( Gtk . A p p l i c a t i o n ) :
def _ _ i n i t _ _ ( s e l f ) :Gtk . A p p l i c a t i o n . _ _ i n i t _ _ (
s e l f , a p p l i c a t i o n _ i d =" org . example . PythonGnome " )s e l f . connect ( " a c t i v a t e " , s e l f . on_ac t i va te )s e l f . connect ( " s t a r t u p " , s e l f . on_star tup )
def on_star tup ( s e l f , app ) :s e l f . window = Gtk . Appl icat ionWindow ( a p p l i c a t i o n =app )he l lo_wor ld = Gio . SimpleAct ion (
name=" he l lo−world " , parameter_type=None )s e l f . add_act ion ( he l l o_wor ld )
# Continued on next s l i d e .
Applications menus code 2
# Continued from prev ious s l i d e .s e l f . add_acce lera tor (
"<Primary >h " , " app . he l lo−world " , None )he l lo_wor ld . connect ( " a c t i v a t e " , s e l f . on_hel lo_wor ld )
q u i t = Gio . SimpleAct ion (name=" q u i t " , parameter_type=None )
s e l f . add_act ion ( q u i t )s e l f . add_acce lera tor ( "<Primary >q " , " app . q u i t " , None )q u i t . connect ( " a c t i v a t e " , s e l f . on_qui t )
appmenu = Gio . Menu . new ( )sec t ion = Gio . Menu . new ( )he l lo_wor ld_ i tem = Gio . MenuItem . new(
" He l lo wor ld ! " , " app . he l lo−world " )qu i t _ i t em = Gio . MenuItem . new( " Qui t " , " app . q u i t " )appmenu . append_section (None , sec t ion )sec t ion . append_item ( he l lo_wor ld_ i tem )sec t ion . append_item ( qu i t _ i t em )s e l f . set_app_menu (appmenu )
# Continued on next s l i d e .
Application menus code 3
# Continued from prev ious s l i d e .
def on_ac t i va te ( s e l f , app ) :s e l f . window . show_al l ( )
def on_hel lo_wor ld ( s e l f , ac t i on =None , param=None ) :pr in t ( " He l lo wor ld ! " )
def on_qui t ( s e l f , ac t i on =None , param=None ) :s e l f . q u i t ( )
app = PythonApp ( )e x i t _ s t a t u s = app . run (None )sys . e x i t ( e x i t _ s t a t u s )
TM
GNOME application development Summary
Buttons and actionable widgets
As buttons implement the GtkActionable interface, theycan also be linked to actionsSet the action name on the GtkActionable with theaction-name propertyAs GtkWindow is a GtkContainer, use the add()method to put the button in the windowSee the GtkActionable API reference for more details
Buttons and actionable widgets code changes
# Put these code l i n e s i n the r i g h t place .
but ton = Gtk . Button (l a b e l = " He l lo wor ld ! " , action_name=" app . he l lo−world " )
s e l f . window . add ( but ton )
TM
GNOME application development Summary
A simple text editor
Add a GtkEntry (or a GtkTreeView if you are feelingconfident)Save the contents of the text entry when quitting, loadthem on startupNo hints this time, you have to do it yourself!You will find the GtkEntry API reference helpful. Youshould know how to load and save text from a file in Python
TM
GNOME application development Summary
Deploying your application
Install a desktop file and icon to show your applicationalongside othersUse a standard build system to make your application areleasable unitMake regular releases, so that your application can beeasily consumedPackage your application for distributions (learn about thiswith Fedora at FUDCon)Look forward to a future of application sandboxing
Example desktop file
[ Desktop ]Name=My Python AppComment=Short d e s c r i p t i o n o f t h i s a p p l i c a t i o nKeywords=python ; e d i t o r ;Type= A p p l i c a t i o nExec=python−gnome−appIcon=python−gnome−appCategor ies=Gtk ;GNOME; U t i l i t y ;
Example autotools build system (configure.ac)
AC_INIT ( [ Python GNOME App ] ,[ 0 . 1 ] ,[ amigadave@amigadave . com] ,[ python−gnome−app ] ,[ h t t p : / / fedorapeople . org / c g i t / amigadave / p u b l i c _ g i t /
python−gnome−app . g i t / ] )
AM_INIT_AUTOMAKE( [ 1 . 1 1 f o r e i g n ] )
AC_CONFIG_FILES ( [ Makef i le ] )
AC_OUTPUT
Example autotools build system (Makefile.am)
dist_bin_SCRIPTS = python−gnome−app
desk topd i r = $ ( d a t a d i r ) / a p p l i c a t i o n sdist_desktop_DATA = python−gnome−app . desktop
TM
GNOME application development Summary
Using the autotools build system
Run autoreconf --force --install to generate thebuild filesRun ./configure to configure the project and check forrequired dependenciesRun make to build the project, and make install toinstall it into the default prefixRun make distcheck to create a tarball and perform abuild and install check
TM
GNOME application development Summary
Translations
GNOME applications use GNU gettext for marking,extracting and retrieving translatable stringsintltool is currently used for translating desktop files andGSettings schemas, but the latest version of gettext can dothis tooSee the translation guide in the application developmentoverview
TM
GNOME application development Summary
User help
GNOME application documentation is written in theMallard formatDesigned to be concise and task-basedAttend the “GNOME Documentation” talks for moreinformationSee the user help section of the application developmentoverview
TM
GNOME application development Summary
Further resources
Mailing lists, https://mail.gnome.org/Wiki, https://wiki.gnome.org/IRC,https://wiki.gnome.org/Community/GettingInTouch/IRChttps://developer.gnome.org/https://python-gtk-3-tutorial.readthedocs.org/en/latest/
TM
Settings management
GSettings is the API in GIO used for storing userpreferencesSettings are stored in dconf on Unix, the registry onWindowsGAction can be backed by a setting
TM
New widgets
popovers, header bars, and lots more!See my talk “Bringing your GNOME application up-to-date”
Top Related