Book Image

Python GUI Programming Cookbook, Second Edition - Second Edition

By : Burkhard Meier
Book Image

Python GUI Programming Cookbook, Second Edition - Second Edition

By: Burkhard Meier

Overview of this book

Python is a multi-domain, interpreted programming language. It is a widely used general-purpose, high-level programming language. It is often used as a scripting language because of its forgiving syntax and compatibility with a wide variety of different eco-systems. Python GUI Programming Cookbook follows a task-based approach to help you create beautiful and very effective GUIs with the least amount of code necessary. This book will guide you through the very basics of creating a fully functional GUI in Python with only a few lines of code. Each and every recipe adds more widgets to the GUIs we are creating. While the cookbook recipes all stand on their own, there is a common theme running through all of them. As our GUIs keep expanding, using more and more widgets, we start to talk to networks, databases, and graphical libraries that greatly enhance our GUI’s functionality. This book is what you need to expand your knowledge on the subject of GUIs, and make sure you’re not missing out in the long run.
Table of Contents (18 chapters)
Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Creating our first Python GUI


Python is a very powerful programming language. It ships with the built-in tkinter module. In only a few lines of code (four, to be precise) we can build our first Python GUI.

Getting ready

To follow this recipe, a working Python development environment is a prerequisite. The IDLE GUI, which ships with Python, is enough to start. IDLE was built using tkinter!

Note

  • All the recipes in this book were developed using Python 3.6 on a Windows 10 64-bit OS. They have not been tested on any other configuration. As Python is a cross-platform language, the code from each recipe is expected to run everywhere.
  • If you are using a Mac, it does come with built-in Python, yet it might be missing some modules such as tkinter, which we will use throughout this book.
  • We are using Python 3.6, and the creator of Python intentionally chose not to make it backwards compatible with Python 2. If you are using a Mac or Python 2, you might have to install Python 3.6 from www.python.org in order to successfully run the recipes in this book.
  • If you really wish to run the code in this book on Python 2.7, you will have to make some adjustments. For example, tkinter in Python 2.x has an uppercase T. The Python 2.7 print statement is a function in Python 3.6 and requires parentheses.
  • While the EOL (End Of Life) for the Python 2.x branch has been extended to the year 2020, I would strongly recommend that you start using Python 3.6 and above.
  • Why hold on to the past, unless you really have to? Here is a link to the Python Enhancement Proposal (PEP) 373 that refers to the EOL of Python 2: https://www.python.org/dev/peps/pep-0373/

 

 

How to do it…

Here are the four lines of First_GUI.py required to create the resulting GUI:

Execute this code and admire the result:

How it works…

In line nine, we import the built-in tkinter module and alias it as tk to simplify our Python code. In line 12, we create an instance of the Tk class by calling its constructor (the parentheses appended to Tk turns the class into an instance). We are using the alias tk, so we don't have to use the longer word tkinter. We are assigning the class instance to a variable named win (short for a window). As Python is a dynamically typed language, we did not have to declare this variable before assigning to it, and we did not have to give it a specific type. Python infers the type from the assignment of this statement. Python is a strongly typed language, so every variable always has a type. We just don't have to specify its type beforehand like in other languages. This makes Python a very powerful and productive language to program in.

Note

A little note about classes and types:

  • In Python, every variable always has a type. We cannot create a variable that does not have a type. Yet, in Python, we do not have to declare the type beforehand, as we have to do in the C programming language.
  • Python is smart enough to infer the type. C#, at the time of writing this book, also has this capability. Using Python, we can create our own classes using the class keyword instead of the def keyword.
  • In order to assign the class to a variable, we first have to create an instance of our class. We create the instance and assign this instance to our variable, for example:class AClass(object):    print('Hello from AClass') class_instance = AClass()Now, the variable, class_instance, is of the AClass type. If this sounds confusing, do not worry. We will cover OOP in the coming chapters.

In line 15, we use the instance variable (win) of the class to give our window a title via the title property. In line 20, we start the window's event loop by calling the mainloop method on the class instance, win. Up to this point in our code, we created an instance and set one property, but the GUI will not be displayed until we start the main event loop.

Note

  • An event loop is a mechanism that makes our GUI work. We can think of it as an endless loop where our GUI is waiting for events to be sent to it. A button click creates an event within our GUI, or our GUI being resized also creates an event.
  • We can write all of our GUI code in advance and nothing will be displayed on the user's screen until we call this endless loop (win.mainloop() in the preceding code). The event loop ends when the user clicks the red X button or a widget that we have programmed to end our GUI. When the event loop ends, our GUI also ends.

There's more…

This recipe used a minimum amount of Python code to create our first GUI program. However, throughout this book we will use OOP when it makes sense.