Python GUI Programming Cookbook
eBook - ePub

Python GUI Programming Cookbook

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

Burkhard Meier

  1. 486 Seiten
  2. English
  3. ePUB (handyfreundlich)
  4. Über iOS und Android verfügbar
eBook - ePub

Python GUI Programming Cookbook

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

Burkhard Meier

Angaben zum Buch
Buchvorschau
Inhaltsverzeichnis
Quellenangaben

Über dieses Buch

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.

Häufig gestellte Fragen

Wie kann ich mein Abo kündigen?
Gehe einfach zum Kontobereich in den Einstellungen und klicke auf „Abo kündigen“ – ganz einfach. Nachdem du gekündigt hast, bleibt deine Mitgliedschaft für den verbleibenden Abozeitraum, den du bereits bezahlt hast, aktiv. Mehr Informationen hier.
(Wie) Kann ich Bücher herunterladen?
Derzeit stehen all unsere auf Mobilgeräte reagierenden ePub-Bücher zum Download über die App zur Verfügung. Die meisten unserer PDFs stehen ebenfalls zum Download bereit; wir arbeiten daran, auch die übrigen PDFs zum Download anzubieten, bei denen dies aktuell noch nicht möglich ist. Weitere Informationen hier.
Welcher Unterschied besteht bei den Preisen zwischen den Aboplänen?
Mit beiden Aboplänen erhältst du vollen Zugang zur Bibliothek und allen Funktionen von Perlego. Die einzigen Unterschiede bestehen im Preis und dem Abozeitraum: Mit dem Jahresabo sparst du auf 12 Monate gerechnet im Vergleich zum Monatsabo rund 30 %.
Was ist Perlego?
Wir sind ein Online-Abodienst für Lehrbücher, bei dem du für weniger als den Preis eines einzelnen Buches pro Monat Zugang zu einer ganzen Online-Bibliothek erhältst. Mit über 1 Million Büchern zu über 1.000 verschiedenen Themen haben wir bestimmt alles, was du brauchst! Weitere Informationen hier.
Unterstützt Perlego Text-zu-Sprache?
Achte auf das Symbol zum Vorlesen in deinem nächsten Buch, um zu sehen, ob du es dir auch anhören kannst. Bei diesem Tool wird dir Text laut vorgelesen, wobei der Text beim Vorlesen auch grafisch hervorgehoben wird. Du kannst das Vorlesen jederzeit anhalten, beschleunigen und verlangsamen. Weitere Informationen hier.
Ist Python GUI Programming Cookbook als Online-PDF/ePub verfügbar?
Ja, du hast Zugang zu Python GUI Programming Cookbook von Burkhard Meier im PDF- und/oder ePub-Format sowie zu anderen beliebten Büchern aus Computer Science & Desktop Applications. Aus unserem Katalog stehen dir über 1 Million Bücher zur Verfügung.

Information

Jahr
2019
ISBN
9781838828813

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...

Inhaltsverzeichnis

Zitierstile für Python GUI Programming Cookbook

APA 6 Citation

Meier, B. (2019). Python GUI Programming Cookbook (3rd ed.). Packt Publishing. Retrieved from https://www.perlego.com/book/1204143/python-gui-programming-cookbook-develop-functional-and-responsive-user-interfaces-with-tkinter-and-pyqt5-3rd-edition-pdf (Original work published 2019)

Chicago Citation

Meier, Burkhard. (2019) 2019. Python GUI Programming Cookbook. 3rd ed. Packt Publishing. https://www.perlego.com/book/1204143/python-gui-programming-cookbook-develop-functional-and-responsive-user-interfaces-with-tkinter-and-pyqt5-3rd-edition-pdf.

Harvard Citation

Meier, B. (2019) Python GUI Programming Cookbook. 3rd edn. Packt Publishing. Available at: https://www.perlego.com/book/1204143/python-gui-programming-cookbook-develop-functional-and-responsive-user-interfaces-with-tkinter-and-pyqt5-3rd-edition-pdf (Accessed: 14 October 2022).

MLA 7 Citation

Meier, Burkhard. Python GUI Programming Cookbook. 3rd ed. Packt Publishing, 2019. Web. 14 Oct. 2022.