Reader small image

You're reading from  Python GUI Programming Cookbook. - Third Edition

Product typeBook
Published inOct 2019
Reading LevelIntermediate
Publisher
ISBN-139781838827540
Edition3rd Edition
Languages
Right arrow
Author (1)
Burkhard Meier
Burkhard Meier
author image
Burkhard Meier

Burkhard Meier is a professional software test automation designer, developer, and analyst. He has more than 17 years' professional experience working for several software companies in California, USA. He is the author of Python GUI Programming Cookbook, First and Second Edition. This book is also available as a Packt video course. He is also the author of the Python Projects Packt video course. In his professional career, he developed advanced in-house testing frameworks written in Python 3. He also developed advanced test automation GUIs in Python, which highly increased the productivity of the software development testing team. When not dreaming in Python code, he reads programming books about design, likes to go for long walks, and reads classical poetry.
Read more about Burkhard Meier

Right arrow

Best Practices

In this chapter, we will explore the different best practices that can help us to build our GUI efficiently and keep it both maintainable and extendable.

These best practices will also help you to debug your GUI to get it just the way you want it to be.

Here is the overview of Python modules for this chapter:

Knowing how to code using best practices will greatly enhance your Python programming skills.

The recipes that will be discussed in this chapter are the following:

    • Avoiding spaghetti code
    • Using __init__ to connect modules
    • Mixing fall-down and OOP coding
    • Using a code naming convention
    • When not to use OOP
    • How to use design patterns successfully
    • Avoiding complexity
    • GUI design using multiple notebooks

Avoiding spaghetti code

In this recipe, we will explore a typical way to create spaghetti code and then we will see a much better way of how to avoid such code.

Spaghetti code is code in which a lot of functionality is intertangled.

Getting ready

We will create a new, simple GUI, written in Python using the built-in Python tkinkter library.

How to do it...

Having searched online and read the documentation, we might start by writing the following code to create our GUI:

  1. Create a new module: GUI_Spaghetti.py.
  2. Add the following code:
# Spaghetti Code ##################...

Using __init__ to connect modules

When we create a new Python package using the PyDev plugin for the Eclipse IDE, it automatically creates an __init__.py module. We can also create it ourselves manually, when not using Eclipse.

The __init__.py module is usually empty and, then, has a size of 0 KB.

We can use this usually empty module to connect different Python modules by entering code into it. This recipe will show how to do this.

Getting ready

We will create a new GUI similar to the one we created in the previous recipe, Avoiding spaghetti code.

How to do it...

As our...

Mixing fall-down and OOP coding

Python is an OOP language, yet it does not always make sense to use OOP. For simple scripting tasks, the legacy waterfall coding style is still appropriate.

In this recipe, we will create a new GUI that mixes both the fall-down coding style with the more modern OOP coding style.

We will create an OOP-style class that will display a tooltip when we hover the mouse over a widget in a Python GUI, which we will create using the waterfall style.

Fall-down and waterfall coding styles are the same. It means that we have to physically place code above code before we can call it from the code below. In this paradigm, the code literally falls down from the top of our program to the bottom of our program when we execute the code.

Getting ready

...

Using a code naming convention

This recipe will show you the value of adhering to a code naming scheme: it helps us to find the code we want to extend, and reminds us of the design of our program.

Getting ready

In this recipe, we will look at Python module names and look at good naming conventions.

How to do it...

We will create example projects with different Python module names to compare the naming:

  1. Create a new ModuleNames folder under Folder1.
  2. Add the following Python modules, 1, 11, 2, and 3:

  1. Next, create a new ModuleNames_ folder under Folder1.
  2. Add the following...

When not to use OOP

Python comes built in with OOP capabilities, but at the same time, we can write scripts that do not need to use OOP. For some tasks, OOP does not make sense.

This recipe will show us when not to use OOP.

Getting ready

In this recipe, we will create a Python GUI similar to the previous recipes. We will compare the OOP code to the non-OOP alternative way of programming the GUI. The resultant output will be the same but the code of the two versions is slightly different.

How to do it...

Let's see how to perform this recipe:

  1. Let's first create...

How to use design patterns successfully

In this recipe, we will create widgets for our Python GUI by using the factory design pattern. In the previous recipes, we created our widgets either manually one at a time or dynamically in a loop. Using the factory design pattern, we will use the factory to create our widgets.

Getting ready

We will create a Python GUI that has three buttons, each having a different style.

How to do it...

We will create a Python GUI with different button styles and we will use a factory design pattern to create these different styles:

  1. Create a...

Avoiding complexity

In this recipe, we will extend our Python GUI and learn ways to handle the increasing complexity of our software development efforts.

Our co-workers and clients love the GUIs we create in Python and ask for more and more features to add to our GUI.

This increases complexity and can easily ruin our original nice design.

Getting ready

We will create a new Python GUI similar to those in the previous recipes and will add many features to it in the form of widgets.

How to do it...

Let's see how to perform the recipe:

  1. We will start with a Python GUI...

GUI design using multiple notebooks

In this recipe, we will create our GUI using multiple notebooks. Surprisingly, tkinter does not ship out of the box with this functionality, but we can design such a widget ourselves.

Using multiple notebooks will further reduce the complexity discussed in the previous recipe.

Getting ready

We will create a new Python GUI similar to the one in the previous recipe. This time, however, we will design our GUI with two notebooks. To focus on this feature, we will use functions instead of class methods. Reading the previous recipe will be a good introduction to this recipe.

How to do it...

...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Python GUI Programming Cookbook. - Third Edition
Published in: Oct 2019Publisher: ISBN-13: 9781838827540
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Burkhard Meier

Burkhard Meier is a professional software test automation designer, developer, and analyst. He has more than 17 years' professional experience working for several software companies in California, USA. He is the author of Python GUI Programming Cookbook, First and Second Edition. This book is also available as a Packt video course. He is also the author of the Python Projects Packt video course. In his professional career, he developed advanced in-house testing frameworks written in Python 3. He also developed advanced test automation GUIs in Python, which highly increased the productivity of the software development testing team. When not dreaming in Python code, he reads programming books about design, likes to go for long walks, and reads classical poetry.
Read more about Burkhard Meier