Python For Desktop Applications Tran Duc Loi Sample
Python For Desktop Applications Tran Duc Loi Sample
APPLICATIONS
First Edition
Copyright
All right reserved. No part of this book may be reproduced, stored in a retrieval
system, or transmitted in any form or by any means, without the prior written
permission of the publisher, except in the case of brief quotations embedded in
critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy
of the information presented. However, the information contained in this book is
sold without warranty, either express or implied. Neither the author, nor
publisher, and its dealers and distributors will be held liable for any damages
caused or alleged to be caused directly or indirectly by this book.
Revision: 20210725
Asset files used in this book are created by Tran Duc Loi.
About the Author
Tran Duc Loi aka Leo Tran is a fan of minimalism and an enthusiast Python
programmer since 2014 when he accidentally asked to join a new Python
community with some friends. Since then, he found that he has special interest
with Python.
Beside Python, he also added Javascript to his stack with React JS for frontend
and React Native for mobile development.
His style of writing is practical that’s why you’ll find his books full of
illustrations, diagrams and examples with a little of theory.
Table of Contents
Copyright .................................................................................................. 3
Preface ......................................................................................................... 8
Conventions ............................................................................................. 10
PIP ........................................................................................................ 17
Wheels .................................................................................................. 17
GIL ...................................................................................................... 20
Editor....................................................................................................24
Git ........................................................................................................ 25
First Python application.......................................................................... 25
Exercises ...............................................................................................62
Preface
Chapter 3, Create a Music Player with Kivy walks through how make a music
player with Kivy. We will start with a very simple Kivy application then
eventually build a more complex one. We also pack our music player up using
PyInstaller.
8
Python For Desktop Applications First Edition
Please note that to keep the book briefly and to help readers focus on main target
of the book, creating desktop applications, I will not cover tests in this edition.
Depend on received feedback, I will consider to add more features in the next
editions.
If you have known Python before, it would be easier for you to read this book.
If you are a beginner, it still be fine for you to get start Python programming with
this book. You need to read chapter 01 and explore as much as possible to
understand some basic concepts: virtual environment, multithreading, pip,
wheels.
9
Python For Desktop Applications First Edition
This is a practical book. That means you shall need to get your hands dirty.
It’s not about using the examples in this book to make commercial products, they
are here for demonstration and being kept as simple as possible hence they are
not shiny enough to be in production. You need to read them to understand the
fundamental concepts and techniques.
Conventions
This book has some conventions that you should be familiar with in order to get
more from this book.
10
Python For Desktop Applications First Edition
This is information box, tips and tricks. I usually explain things and answer some
common questions here. It is an interesting box.
This is source code of the examples. We need the line number as we will use line
numbers as an anchor to explain the code.
File names, commands, folder names usually use bold format with different font
family than normal text just to emphasize them.
11
Python For Desktop Applications First Edition
Reader feedback
All feedback about the content of the book should be sent to the Telegram group
at https://t.me/py4da or emailed directly to the author at [email protected].
Let us know what you think about this book – what you liked or may have
disliked.
Reader feedback is important for us to develop titles that you really get the most
out of.
12
Python For Desktop Applications First Edition
13
Python For Desktop Applications First Edition
14
Python For Desktop Applications First Edition
Chapter 1: Introduction
1.1. Introduction
15
Python For Desktop Applications First Edition
def func(): …
Original .py file
Compiled by
1 3 2
PyPy, PythonNet,
CPython (official)
Jython Compiler IronPython Compiler wpython, Brython,
Compiler
Hotpy, ...
Dynamic Language
Runtime (DLR) on top
CPython Interpreter/
Jython Interpreter/ of Common
Cpython Virtual
Java Virtual Machine Language Runtime
Machine
(CLR)/.NET Virtual
Machine
As PyPy wrote on their home page, PyPy is 4.2 times faster than CPython.
16
Python For Desktop Applications First Edition
PIP
pip is a package installer for Python. You can use pip to install packages from the
Python Package Index and other indexes.
By default, pip is shipped with Python installer so you don’t have to install pip
manually.
You can check your current pip version with pip --version command:
Please note that pip will be created per virtual environment as you can see in the
output above.
Wheels
Wheels are new standards of Python distribution. Please note that wheels are
supported from pip >= 1.4 and setuptools >= 0.8.
Wheels end with .whl extension. You can easily find wheels at any project’s
download tab as below:
17
Python For Desktop Applications First Edition
Once you have wheels downloaded, you can install the package locally using:
Wheels are extreme helpful for some kind of packages those don’t have any
wheels available at pypi.org. There is my experience with wheels at Python and
choosing the right version at section 1.2 in this chapter.
Virtual Environment
In this book, you will use a separated virtual environments for each chapter. E.g:
chapter 01 will have its own virtual environment inside chapter01 directory.
Python has a built-in virtual environment package named venv to allow you
create a virtual environment:
18
Python For Desktop Applications First Edition
The above command creates a virtual environment into a folder inside current
folder named venv.
To use the newly created virtual environment, you have to activate it using
activate command.
After activating process completed, there will be a prefix (venv) at the beginning
of command prompt to let you know that you are in a virtual environment.
19
Python For Desktop Applications First Edition
To install a package inside a virtual environment, you can use pip command as
normal. The packages will be installed at venv/Lib directory.
Python built-in venv package does NOT allow you to specify other Python
versions. To overcome this, you need to specify full path to Python
executable file when using the creation command. E.g:
GIL
GIL stands for Global Interpreter Lock, in CPython, is a mutex that protects
access to Python objects, preventing multiple threads from executing Python
bytecodes at once. This lock exists mainly because CPython memory management
is not thread-safe.
Because of GIL, Python has a bottleneck and can’t taking full advantage of
multiprocessor systems.
20
Python For Desktop Applications First Edition
Please take a look back at the beginning of this chapter for Python
implementations.
Computers can display information and let the user give commands to it using
two methods: a command line interface (CLI) or a graphical user interface (GUI).
In a command line interface or console, the user types commands using the
keyboard to tell the computer to take an action.
In a graphical user interface, the user can use the computer mouse to click on
buttons.
21
Python For Desktop Applications First Edition
22