Python GUI Programming Cookbook

Over 80 object-oriented recipes to help you create mind-blowing GUIs in Python

Burkhard A. Meier

e-books shop
e-books shop
Purchase Now !
Just with Paypal

Book Details
 351 p
 File Size 
 9,239 KB
 File Type
 PDF format
 2015 Packt Publishing 

About the Author
Burkhard A. Meier has more than 15 years of professional experience working in the
software industry as a software tester and developer, specializing in software test automation
development, execution, and analysis. He has a very strong background in SQL relational
database administration, the development of stored procedures, and debugging code.
While experienced in Visual Studio .NET C#, Visual Test, TestComplete, and other testing
languages (such as C/C++), the main focus of the author over the past two years has been
developing test automation written in Python 3 to test the leading edge of FLIR ONE infrared
cameras for iPhone and Android smart phones as well as handheld tablets.

Being highly appreciative of art, beauty, and programming, the author developed GUIs in C# and
Python to streamline everyday test automation tasks, enabling these automated tests to run
unattended for weeks, collecting very useful data to be analyzed and automatically plotted into
graphs and e-mailed to upper management upon completion of nightly automated test runs.
His previous jobs include working as a senior test automation engineer and designer for
InfoGenesis (now Agilysys), QAD, InTouch Health, and presently, FLIR Systems.
You can get in touch with him through his LinkedIn account, 

About the Reviewers
Joy Bindroo holds a bachelor's degree in computer science and engineering. He is currently
pursuing his post-graduate studies in the field of information management. He is a creative
person and enjoys working on Linux platform and other open source technologies. He
enjoys writing about Python and sharing his ideas and skills on his website,
He likes to sketch, write poems, listen to music, and have fun with his
friends in his free time.

Peter Bouda works as a senior web developer for MAJ Digital and is a specialist in
full stack JavaScript applications based on LoopBack and AngularJS. He develops Python
GUIs for companies and research projects since 2003 and wrote a German book, PyQt und
PySide – GUI- und Anwendungsentwicklung mit Python und Qt, on Python GUI development,
which was published in 2012. Currently, he is getting crazy with embedded and open
hardware platforms and is working on a modular game console based on open hardware.

Joseph Rex is a full stack developer with a background in computer security. He has worked
on Python GUI products and some CLI programs to experiment with information security. He
came out of security to web development and developed a passion for rails and JavaScript MVC
frameworks after working on several projects using jQuery. He has been in the web industry
for 3 years, building web applications and mobile apps. He has also written articles on security
for InfoSec Institute and has written some scripts to back them up. He has to his credit several
personal experimental projects written in Python.

In this book, we will explore the beautiful world of graphical user interfaces (GUIs) 
using the Python programming language.
Along the way, we will talk to networks, queues,
 the OpenGL graphical library, and many more technologies.
This is a programming cookbook. Every chapter is self-contained 
and explains a certain programming solution.
We will start very simply, yet throughout this book 
we will build a working program written in Python 3.
We will also apply some design patterns and use best practices throughout this book.
The book assumes that the reader has some basic experience using the Python
programming language, but that is not really required to use this book.
If you are an experienced programmer in any programming language, you will have a fun

time extending your skills to programming GUIs using Python!

Are you ready?

Let's start on our journey…

What you need for this book
All required software for this book is available online and is free of charge. This starts with
Python 3 itself, and then extends to Python's add-on modules. In order to download any
required software, you will need a working Internet connection.

Table of Contents
Preface v
Chapter 1: Creating the GUI Form and Adding Widgets 1
Introduction 1
Creating our first Python GUI 2
Preventing the GUI from being resized 4
Adding a label to the GUI form 6
Creating buttons and changing their text property 7
Text box widgets 9
Setting the focus to a widget and disabling widgets 11
Combo box widgets 12
Creating a check button with different initial states 14
Using radio button widgets 16
Using scrolled text widgets 18
Adding several widgets in a loop 20
Chapter 2: Layout Management 23
Introduction 23
Arranging several labels within a label frame widget 24
Using padding to add space around widgets 26
How widgets dynamically expand the GUI 28
Aligning the GUI widgets by embedding frames within frames 32
Creating menu bars 36
Creating tabbed widgets 41
Using the grid layout manager 46
Chapter 3: Look and Feel Customization 49
Introduction 49
Creating message boxes – information, warning, and error 50
How to create independent message boxes 53
How to create the title of a tkinter window form 56
Changing the icon of the main root window 57
Using a spin box control 58
Relief, sunken, and raised appearance of widgets 61
Creating tooltips using Python 63
How to use the canvas widget 67
Chapter 4: Data and Classes 69
Introduction 69
How to use StringVar() 69
How to get data from a widget 73
Using module-level global variables 75
How coding in classes can improve the GUI 79
Writing callback functions 85
Creating reusable GUI components 86
Chapter 5: Matplotlib Charts 91
Introduction 91
Creating beautiful charts using Matplotlib 92
Matplotlib – downloading modules using pip 94
Matplotlib – downloading modules with whl extensions 98
Creating our first chart 100
Placing labels on charts 102
How to give the chart a legend 106
Scaling charts 109
Adjusting the scale of charts dynamically 112
Chapter 6: Threads and Networking 117
Introduction 117
How to create multiple threads 118
Starting a thread 121
Stopping a thread 125
How to use queues 128
Passing queues among different modules 133
Using dialog widgets to copy files to your network 136
Using TCP/IP to communicate via networks 145
Using URLOpen to read data from websites 147
Chapter 7: Storing Data in Our MySQL Database via Our GUI 153
Introduction 153
Connecting to a MySQL database from Python 154
Configuring the MySQL connection 157
Designing the Python GUI database 161
Using the SQL INSERT command 168
Using the SQL UPDATE command 172
Using the SQL DELETE command 177
Storing and retrieving data from our MySQL database 181
Chapter 8: Internationalization and Testing 187
Introduction 187
Displaying widget text in different languages 188
Changing the entire GUI language all at once 191
Localizing the GUI 196
Preparing the GUI for internationalization 201
How to design a GUI in an agile fashion 204
Do we need to test the GUI code? 208
Setting debug watches 212
Configuring different debug output levels 216
Creating self-testing code using
Python's __main__ section 220
Creating robust GUIs using unit tests 224
How to write unit tests using the Eclipse PyDev IDE 229
Chapter 9: Extending Our GUI with the wxPython Library 235
Introduction 235
How to install the wxPython library 236
How to create our GUI in wxPython 239
Quickly adding controls using wxPython 244
Trying to embed a main wxPython app in a main tkinter app 251
Trying to embed our tkinter GUI code into wxPython 253
How to use Python to control two different GUI frameworks 256
How to communicate between the two connected GUIs 260
Chapter 10: Creating Amazing 3D GUIs with PyOpenGL and PyGLet 265
Introduction 265
PyOpenGL transforms our GUI 266
Our GUI in 3D! 270
Using bitmaps to make our GUI pretty 275
PyGLet transforms our GUI more easily than PyOpenGL 279
Our GUI in amazing colors 283
Creating a slideshow using tkinter 286
Chapter 11: Best Practices 291
Introduction 291
Avoiding spaghetti code 291
Using __init__ to connect modules 298
Mixing fall-down and OOP coding 305
Using a code naming convention 310
When not to use OOP 314
How to use design patterns successfully 317
Avoiding complexity 320
Index 327

e-books shop

Who this book is forI
This book is for programmers who wish to create a graphical user interface (GUI). You might
be surprised by what we can achieve by creating beautiful, functional, and powerful GUIs
using the Python programming language. Python is a wonderful, intuitive programming
language, and is very easy to learn.

I like to invite you to start on this journey now. It will be a lot of fun!
Loading... Protection Status