Table Of ContentCreate Simple GUI Applications, with Python &
Qt5
The hands-on guide to building desktop apps with
Python.
Martin Fitzpatrick
Thisbookisforsaleathttp://leanpub.com/create-simple-gui-applications
Thisversionwaspublishedon2019-08-11
ThisisaLeanpubbook.Leanpubempowersauthorsandpublisherswiththe
LeanPublishingprocess.LeanPublishingistheactofpublishinganin-progress
ebookusinglightweighttoolsandmanyiterationstogetreaderfeedback,pivot
untilyouhavetherightbookandbuildtractiononceyoudo.
ThisworkislicensedunderaCreativeCommons
Attribution-NonCommercial-ShareAlike3.0UnportedLicense
Tweet This Book!
PleasehelpMartinFitzpatrickbyspreadingthewordaboutthisbookonTwitter!
Thesuggestedhashtagforthisbookis#createsimpleguis.
Findoutwhatotherpeoplearesayingaboutthebookbyclickingonthislinkto
searchforthishashtagonTwitter:
#createsimpleguis
Contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Bookformat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
QtandPyQt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Python3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
GettingStarted. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
InstallationWindows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
PyQt5forPython3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
PyQt5forPython2.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
InstallationMac . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
InstallationLinux(Ubuntu) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
BasicQtFeatures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
MyfirstWindow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Signals,Slots,Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Actions,ToolbarsandMenus. . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Layouts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
QtCreator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Creatinga.uifile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
LayingoutyourMainWindow . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Usingyourgenerated.uifile . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Addingapplicationlogic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
ExtendedSignals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
ModifyingSignalData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
CustomSignals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
CONTENTS
QPainterandBitmapGraphics . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
QPainter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Drawingprimitives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
AbitoffunwithQPainter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
CreatingCustomWidgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Gettingstarted . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
paintEvent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Positioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Updatingthedisplay. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Drawingthebar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
CustomisingtheBar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
AddingtheQAbstractSliderInterface . . . . . . . . . . . . . . . . . . . . . 151
UpdatingfromtheMeterdisplay . . . . . . . . . . . . . . . . . . . . . . . . 152
Thefinalcode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
TheModelViewArchitecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
ModelViewController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
TheModelView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
AsimpleModelView—aTodoList . . . . . . . . . . . . . . . . . . . . . . . 160
Apersistentdatastore . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Multithreading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Preparation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Thedumbapproach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
ThreadsandProcesses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
QRunnableandQThreadPool . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
ExtendedRunners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
ThreadIO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
QRunnableExamples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
ExamplePyQt5Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
MozzarellaAshbadger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Moonsweeper. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
PackagingPyQtApplications . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
fbs:fmanBuildSystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
What’snext? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
CONTENTS
Thevideocourse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Tutorials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Iconsets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Sourcecode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Copyright . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Introduction
WelcometoCreateSimpleGUIApplicationsthepracticalguidetobuildingprofes-
sionaldesktopapplicationswithPython&Qt.
If you want to learn how to write GUI applications it can be pretty tricky to get
started. There are a lot of new concepts you need to understand to get anything
to work. A lot of tutorials offer nothing but short code snippets without any
explanation of the underlying systems and how they work together. But, like any
code,writingGUIapplicationsrequiresyoutolearntothinkabouttheproblemin
therightway.
InthisbookIwillgiveyoutherealusefulbasicsthatyouneedtogetbuildingfunc-
tional applications with the PyQt framework. I’ll include explanations, diagrams,
walkthroughs and code to make sure you know what you’re doing every step of
theway.InnotimeatallyouwillhaveafullyfunctionalQtapplication-readyto
customiseasyoulike.
Thesourcecodeforeachstepisincluded,butdon’tjustcopyandpasteandmove
on.Youwilllearnmuchmoreifyouexperimentalongtheway!
So,let’sgetstarted!
Book format
This book is formatted as a series of coding exercises and snippets to allow you
tograduallyexploreandlearnthedetailsofPyQt5.However,itisnotpossibleto
giveyouacompleteoverviewoftheQtsysteminabookofthissize(it’shuge,this
isn’t),soyouareencouragedtoexperimentandexplorealongtheway.
Ifyoufindyourselfthinking“IwonderifIcandothat”thebestthingyoucandois
putthisbookdown,thengoandfindout!Justkeepregularbackupsofyourcode
alongthewaysoyoualwayshavesomethingtocomebacktoifyouroyallymess
itup.
Introduction 2
Throughoutthisbookstherearealsoboxeslikethis,givinginfo,tipsand
warnings.Allofthemcanbesafelyskippedoverifyouareinahurry,but
readingthemwillgiveyouadeeperandmoreroundedknowledgeofthe
Qtframework.
Qt and PyQt
When you write applications using PyQt what you area really doing is writing
applicationsinQt.ThePyQtlibraryissimply¹awrapperaroundtheC++Qtlibrary,
toallowittobeusedinPython.
Because this is a Python interface to a C++ library the naming conventions used
withinPyQtdonotadheretoPEP8standards.Mostnotablyfunctionsandvariables
are named using mixedCase rather than snake_case. Whether you adhere to this
standardinyourownapplicationsbasedonPyQtisentirelyuptoyou,howeveryou
mayfinditusefultohelpclarifywherethePyQtcodeendsandyourownbegins.
Further, while there is PyQt specific documentation available, you will often find
yourself reading the Qt documentation itself as it is more complete. If you do
you will need to translate object syntax and some methods containing Python-
reservedfunctionnamesasfollows:
Qt PyQt
Qt::SomeValue Qt.SomeValue
object.exec() object.exec_()
object.print() object.print_()
Python 3
ThisbookiswrittentobecompatiblewithPython3.4+.Python3isthefutureof
thelanguage,andifyou’restartingoutnowiswhereyoushouldbefocusingyour
efforts.However,inrecognitionofthefactthatmanypeoplearestucksupporting
or developing on legacy systems, the examples and code used in this book are
also tested and confirmed to work on Python 2.7. Any notable incompatibility or
¹Notreallythatsimple.
Introduction 3
gotchaswillbeflaggedwithameh-facetoaccuratelyportraythesentimente.g.
Python2.7
InPython2.7map()returnsalist.
IfyouareusingPython3youcansafelyignoretheirindifferentgaze.
Getting Started
Before you start coding you will first need to have a working installation of PyQt
andQtonyoursystem.Thefollowingsectionswillguideyouthroughthisprocess
forthemainavailableplatforms.IfyoualreadyhaveaworkinginstallationofPyQt
onyourPythonsystemyoucansafelyskipthispartandgetstraightontothefun.
Thecompletesourcecodeallexamplesinthisbookisavailabletodownloadfrom
here.
GPLOnly
NotethatthefollowinginstructionsareonlyforinstallationoftheGPL
licensedversionofPyQt.IfyouneedtousePyQtinanon-GPLprojectyou
willneedtopurchaseanalternativelicensefromRiverbankComputing
inordertoreleaseyoursoftware.
Documentation?
ThePyQtpackagesfromRiverbankdonotincludetheQtdocumentation.
Howeverthisisavailableonlineatdocs.qt.io.Ifyoudowanttodownload
thedocumentationyoucandosofromwww.qt.io.
Installation Windows
PyQt5forWindowscanbeinstalledasforanyotherapplicationorlibrary.Theonly
slightcomplicationisthatyoumustfirstdeterminewhetheryoursystemsupports
32bit or 64bit software. You can determine whether your system supports 32bit
or64bitbylookingattheSystempanelaccessiblefromthecontrolpanel.