Python GUI Programming Cookbook
eBook - ePub

Python GUI Programming Cookbook

Develop functional and responsive user interfaces with tkinter and PyQt5, 3rd Edition

Burkhard Meier

Share book
  1. 486 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Python GUI Programming Cookbook

Develop functional and responsive user interfaces with tkinter and PyQt5, 3rd Edition

Burkhard Meier

Book details
Book preview
Table of contents
Citations

About This Book

Over 90 recipes to help you develop widgets, forms, layouts, charts, and much more using the latest features of Python 3

Key Features

  • Use object-oriented programming to develop impressive GUIs in Python
  • Create interesting charts to visually represent data using Matplotlib
  • Develop GUIs with the latest versions of tkinter, PyQt5, and wxPython frameworks

Book Description

Python is a multi-domain, interpreted programming language that is easy to learn and implement. With its wide support for frameworks to develop GUIs, you can build interactive and beautiful GUI-based applications easily using Python. This third edition of Python GUI Programming Cookbook follows a task-based approach to help you create effective GUIs with the smallest amount of code. Every recipe in this book builds upon the last to create an entire, real-life GUI application. These recipes also help you solve problems that you might encounter while developing GUIs. This book mainly focuses on using Python's built-in tkinter GUI framework. You'll learn how to create GUIs in Python using simple programming styles and object-oriented programming (OOP). As you add more widgets and expand your GUI, you will learn how to connect to networks, databases, and graphical libraries that greatly enhance the functionality of your GUI. You'll also learn how to use threading to ensure that your GUI doesn't become unresponsive. Toward the end, you'll learn about the versatile PyQt GUI framework, which comes along with its own visual editor that allows you to design GUIs using drag and drop features. By the end of the book, you'll be an expert in designing Python GUIs and be able to develop a variety of GUI applications with ease.

What you will learn

  • Create amazing GUIs with Python's built-in tkinter module
  • Customize GUIs using layout managers to arrange GUI widgets
  • Advance from the typical waterfall coding style to an OOP style using Python
  • Develop beautiful charts using the free Matplotlib Python module
  • Use threading in a networked environment to make GUIs responsive
  • Discover ways to connect GUIs to a MySQL database
  • Understand how unit tests can be created and internationalize GUI
  • Delve into the world of GUI creation using PyQt5

Who this book is for

If you're a programmer or developer looking to enhance your Python skills by writing powerful GUI applications, this book is for you. Familiarity with the Python programming language is necessary to get the most out of the book.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Python GUI Programming Cookbook an online PDF/ePUB?
Yes, you can access Python GUI Programming Cookbook by Burkhard Meier in PDF and/or ePUB format, as well as other popular books in Computer Science & Desktop Applications. We have over one million books available in our catalogue for you to explore.

Information

Year
2019
ISBN
9781838828813
Edition
3

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 ############################# 
def PRINTME(me):print(me)
import tkinter
x=y=z=1
PRINTME(z)
from tkinter import *
scrolW=30;scrolH=6
win=tkinter.Tk()
if x:chVarUn=tkinter.IntVar()
from tkinter import ttk
WE='WE'
import tkinter.scrolledtext
outputFrame=tkinter.ttk.LabelFrame(win,text=' Type into the scrolled text control: ')
scr=tkinter.scrolledtext.ScrolledText(outputFrame,width=scrolW,height=scrolH,wrap=tkinter.WORD)
e='E'
scr.grid(column=1,row=1,sticky=WE)
outputFrame.grid(column=0,row=2,sticky=e,padx=8)
lFrame=None
if y:chck2=tkinter.Checkbutton(lFrame,text="Enabled",variable=chVarUn)
wE='WE'
if y==x:PRINTME(x)
lFrame=tkinter.ttk.LabelFrame(win,text="Spaghetti")
chck2.grid(column=1,row=4,sticky=tkinter.W,columnspan=3)
PRINTME(z)
lFrame.grid(column=0,row=0,sticky=wE,padx=10,pady=10)
chck2.select()
try: win.mainloop()
except:PRINTME(x)
chck2.deselect()
if y==x:PRINTME(x)
# End Pasta #############################
  1. Run the code and observe the output, as follows:

  1. Compare the preceding GUI to the intended GUI design, as follows:
  1. Create a new module, GUI_NOT_Spaghetti.py, and add the following code:
#====================== 
# imports
#======================
import tkinter as tk
from tkinter import ttk
from tkinter import scrolledtext
#======================
# Create instance
#======================
win = tk.Tk()
#======================
# Add a title
#======================
win.title("Python GUI")
#=========================
# Disable resizing the GUI
#=========================
win.resizable(0,0)

  1. Next, add some controls:
#============================================================= 
# Adding a LabelFrame, Textbox (Entry) and Combobox
#=============================================================
lFrame = ttk.LabelFrame(win, text="Python GUI Programming Cookbook")
lFrame.grid(column=0, row=0, sticky='WE', padx=10, pady=10)
#=============================================================
# Using a scrolled Text control
#=============================================================
outputFrame = ttk.LabelFrame(win, text=' Type into the scrolled text
control: ')
outputFrame.grid(column=0, row=2, sticky='E', padx=8)
scrolW = 30
scrolH = 6
scr = scrolledtext.ScrolledText(outputFrame, width=scrolW,
height=scrolH, wrap=tk.WORD)
scr.grid(column=1, row=0, sticky='WE')
  1. Add some more widgets:
#============================================================= 
# Creating a checkbutton
#=============================================================
chVarUn = tk.IntVar()
check2 = tk.Checkbutton(lFrame, text="Enabled", variable=chVarUn)
check2.deselect()
check2.grid(column=1, row=4, sticky=tk.W, columnspan=3)
#======================
# Start GUI
#======================
win.mainloop()
  1. Run the code and observe the following output:
Let's go behind the scenes to understand the code better.

How it works...

While the spaghetti code created a GUI, it is very hard to read because there is so much confusion in the code. Good code has many advantages over spaghetti code.
Let's see an example of spaghetti code first:
def PRINTME(me):print(me) 
import tkinter
x=y=z=1
PRINTME(z)
from tkinter import *
Now, consider this example good code (note that there is not much confusion in reading the code):
#====================== 
# imports
#======================
import tkinter as tk
from tkinter import ttk
The good code has a clearly commented section. We can easily find the import statements:
#-----------------------------------
Consider the following spaghetti code:
import tkinter.scrolledtext 
outputFrame=tkinter.ttk.LabelF...

Table of contents