Python GUI Programming - A Complete Reference Guide

Book description

Explore Python's GUI frameworks and create visually stunning and feature-rich applications

Key Features

  • Integrate stunning data visualizations using Tkinter Canvas and Matplotlib
  • Understand the basics of 2D and 3D animation in GUI applications
  • Explore PyQt's powerful features to easily design and customize your GUI applications

Book Description

A responsive graphical user interface (GUI) helps you interact with your application, improves user experience, and enhances the efficiency of your applications. With Python, you'll have access to elaborate GUI frameworks that you can use to build interactive GUIs that stand apart from the rest.

This Learning Path begins by introducing you to Tkinter and PyQt, before guiding you through the application development process. As you expand your GUI by adding more widgets, you'll work with networks, databases, and graphical libraries that enhance its functionality. You'll also learn how to connect to external databases and network resources, test your code, and maximize performance using asynchronous programming. In later chapters, you'll understand how to use the cross-platform features of Tkinter and Qt5 to maintain compatibility across platforms. You'll be able to mimic the platform-native look and feel, and build executables for deployment across popular computing platforms.

By the end of this Learning Path, you'll have the skills and confidence to design and build high-end GUI applications that can solve real-world problems.

This Learning Path includes content from the following Packt products:

  • Python GUI Programming with Tkinter by Alan D. Moore
  • Qt5 Python GUI Programming Cookbook by B. M. Harwani

What you will learn

  • Visualize graphs in real time with Tkinter's animation capabilities
  • Use PostgreSQL authentication to ensure data security for your application
  • Write unit tests to avoid regression when updating code
  • Handle different signals generated on mouse clicks using QSpinBox and sliders
  • Employ network concepts, internet browsing, and Google Maps in UI
  • Use graphics rendering to implement animations in your GUI

Who this book is for

If you're an intermediate Python programmer looking to enhance your coding skills by writing powerful GUIs in Python using PyQT and Tkinter, this is an ideal Learning Path for you. A strong understanding of the Python language is a must to grasp the concepts explained in this book.

Table of contents

  1. Title Page
  2. Copyright
    1. Python GUI Programming - A Complete Reference Guide
  3. About Packt
    1. Why Subscribe?
    2. Packt.com
  4. Contributors
    1. About the Authors
    2. Packt Is Searching for Authors Like You
  5. Preface
    1. Who This Book Is For
    2. What This Book Covers
    3. To Get the Most out of This Book
      1. Download the Example Code Files
      2. Conventions Used
    4. Get in Touch
      1. Reviews
  6. Introduction to Tkinter
    1. Introducing Tkinter and Tk
      1. Choosing Tkinter
      2. Installing Tkinter
        1. Installing Python 3 on Windows
        2. Installing Python 3 on macOS
        3. Installing Python 3 and Tkinter on Linux
    2. Introducing IDLE
      1. Using the shell mode of IDLE
      2. Using the editor mode of IDLE
      3. IDLE as a Tkinter example
    3. Creating a Tkinter Hello World
      1. Creating a better Hello World Tkinter
    4. Summary
  7. Designing GUI Applications with Tkinter
    1. A problem at ABQ AgriLabs
      1. Assessing the problem
      2. Gathering information about the problem
      3. What you found out
        1. Information about the data being collected
        2. Information about the users of the application
    2. Documenting specification requirements
      1. Contents of a simple specification
      2. Writing the ABQ data entry program specification
    3. Designing the application
      1. Exploring Tkinter input widgets
      2. Grouping our fields
      3. Laying out the form
      4. Laying out the application
    4. Summary
  8. Creating Basic Forms with Tkinter and ttk Widgets
    1. Evaluating our technology choices
      1. Choosing a technology
    2. Exploring Tkinter widgets
      1. The Entry widget
      2. The Spinbox widget
      3. The Combobox widget
      4. The Checkbutton widget
      5. The Text widget
      6. The Button widget
      7. The LabelFrame widget
    3. Implementing the application
      1. Saving some time with a LabelInput class
      2. Building the form
        1. Adding LabelFrame and other widgets
        2. Retrieving data from our form
        3. Resetting our form
      3. Building our application class
        1. Saving to CSV
      4. Finishing and testing
    4. Summary
  9. Reducing User Error with Validation and Automation
    1. Validating user input
      1. Strategies to prevent data errors
    2. Validation in Tkinter
      1. Creating a DateEntry widget
    3. Implementing validated widgets in our form
      1. Exploiting the power of multiple inheritance
        1. A validating mixin class
      2. Building our widgets
        1. Requiring data
        2. A Date widget
        3. A better Combobox widget
        4. A range-limited Spinbox widget
        5. Dynamically adjusting the Spinbox range
        6. Updating our form
        7. Displaying errors
        8. Preventing form submission on error
    4. Automating input
      1. Inserting a date
      2. Automating Lab, Time, and Technician
    5. Summary
  10. Planning for the Expansion of Our Application
    1. Separating concerns
      1. The MVC pattern
        1. What is a model?
        2. What is a view?
        3. What is a controller?
      2. Why complicate our design?
    2. Structuring our application directory
      1. Basic directory structure
        1. The abq_data_entry.py file
        2. The README.rst file
          1. ReStructuredText
        3. Populating the docs folder
        4. Making a Python package
    3. Splitting our application into multiple files
      1. Creating the models module
      2. Moving the widgets
      3. Moving the views
        1. Removing redundancy in our view logic
      4. Creating the application file
        1. Running the application
    4. Using version control software
      1. A super-quick guide to using Git
        1. Initializing and configuring a Git repository
        2. Adding and committing code
        3. Viewing and using our commits
    5. Summary
  11. Creating Menus with Menu and Tkinter Dialogs
    1. Solving problems in our application
      1. Deciding how to address these problems
    2. Implementing simple Tkinter dialogs
      1. Tkinter messagebox
      2. Showing the error dialogs
    3. Designing our menu 
      1. Creating menus in Tkinter
      2. Implementing our application menu
      3. Showing an About dialog
      4. Adding the menu functionality in the controller
      5. Handling file selection
    4. Making our settings work
      1. Persisting settings
        1. Building a model for settings persistence
      2. Using the settings model in our application
    5. Summary
  12. Navigating Records with Treeview
    1. Implementing read and update in the model
      1. Adding read and update to our model
        1. Implementing  get_all_records()
        2. Implementing  get_record()
        3. Adding update to save_record()
    2. Implementing a record list view
      1. The ttk Treeview
      2. Implementing our record list with Treeview
        1. Configuring a Treeview widget
        2. Adding a scrollbar
        3. Populating the Treeview
        4. Responding to record selection
    3. Modifying the record form for read and update
      1. Updating __init__()
      2. Adding a load_record() method
    4. Updating the rest of the application
      1. Main menu changes
      2. Connecting the pieces in Application
        1. Adding the RecordList view
        2. Moving the model
        3. Populating the record list
        4. Adding the new callbacks
        5. Cleaning up
      3. Testing our program
    5. Summary
  13. Improving the Look with Styles and Themes
    1. Working with images in Tkinter
      1. Tkinter PhotoImage
        1. Adding the company logo
        2. Setting our Window icon
    2. Styling Tkinter widgets
      1. Widget color properties
        1. Using widget properties on our form
      2. Using tags
        1. Styling our record list with tags
      3. Tkinter fonts
        1. Giving users font options
    3. Styling Ttk widgets
      1. Exploring a Ttk widget
      2. Styling our form labels
        1. Styling input widgets on error
        2. Making our Spinbox a Ttk widget
        3. Updating ValidatedMixin
      3. Setting themes
        1. Building a theme selector
    4. Summary
  14. Creating Automated Tests with unittest
    1. Automated testing basics
      1. A simple unit test
      2. The unittest module
        1. Writing a test case
        2. TestCase assertion methods
        3. Fixtures
        4. Using Mock and patch
        5. Running multiple unit tests
    2. Testing Tkinter code
      1. Managing asynchronous code
      2. Simulating user actions
        1. Specifying an event sequence
      3. Managing focus and grab
      4. Getting widget information
    3. Writing tests for our application
      1. Testing our model
        1. Testing file reading in get_all_records()
        2. Testing file saving in save_record()
        3. More tests
      2. Testing our application
      3. Testing our widgets
        1. Unit testing the ValidatedSpinbox widget 
        2. Integration testing the ValidatedSpinbox widget
        3. Testing our mixin class
    4. Summary
  15. Improving Data Storage with SQL
    1. PostgreSQL
      1. Installing and configuring PostgreSQL
      2. Connecting with psycopg2
    2. SQL and relational database basics
      1. Basic SQL operations
        1. Syntax differences from Python
        2. Defining tables and inserting data
        3. Retrieving data from tables
        4. Updating rows, deleting rows, and more WHERE clauses
        5. Subqueries
        6. Joining tables
        7. Learning more
    3. Modeling relational data
      1. Normalization
      2. The entity-relationship diagrams
      3. Assigning data types
    4. Creating the ABQ database
      1. Creating our tables
        1. Creating the lookup tables
        2. The lab_checks table
        3. The plot_checks table
      2. Creating a view
    5. Integrating SQL into our application
      1. Creating a new model
      2. Adjusting the Application class for the SQL backend
        1. Building a login window
        2. Using the login window
        3. Fixing some model incompatibilities
          1. DataRecordForm creation
          2. Fixing the open_record() method
          3. Fixing the on_save() method
        4. Creating new callbacks
      3. Updating our views for the SQL backend
        1. The data record form
        2. The record list
      4. Last changes
    6. Summary
  16. Connecting to the Cloud
    1. HTTP using urllib
      1. Basic downloading with urllib.request
      2. Creating a download function
        1. Parsing XML weather data
      3. Implementing weather data storage
        1. Creating the SQL table
        2. Implementing the SQLModel.add_weather_data() method
        3. Updating the SettingsModel class
      4. Adding the GUI elements for weather download
    2. HTTP using requests
      1. Installing and using requests
        1. The requests.session() fucntion
        2. The response objects
      2. Implementing API upload
        1. Creating a test HTTP service
        2. Creating our network function
        3. Updating application
        4. Updating the models.py file
        5. Finishing up
    3. FTP using ftplib
      1. Basic concepts of FTP
      2. Creating a test FTP service
      3. Implementing the FTP upload function
        1. Listing files
        2. Retrieving files
        3. Deleting or renaming files
      4. Adding FTP upload to the GUI
    4. Summary
  17. Visualizing Data Using the Canvas Widget
    1. Drawing and animation with Tkinter's Canvas
      1. Animating Canvas objects
        1. Creating our objects
        2. Animating the racers
        3. Detecting and handling a win condition
    2. Creating simple graphs on the canvas
      1. Creating the model method
      2. Creating the graph view
      3. Updating the application
    3. Advanced graphs using Matplotlib and Tkinter
      1. Data model method
      2. Creating the bubble chart view
      3. Application method
    4. Summary
  18. Creating a User Interface with Qt Components
    1. Introduction
      1. PyQt
      2. Ways of creating GUI applications
    2. Displaying a welcome message
      1. Understanding the Label widget
        1. Methods
      2. Understanding the Line Edit widget
        1. Methods
      3. Understanding the Push Button widget
      4. How to do it...
      5. How it works...
    3. Using the Radio Button widget
      1. Understanding Radio Button
        1. Methods 
        2. Signal description
      2. How to do it...
      3. How it works...
    4. Grouping radio buttons
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Displaying options in the form of checkboxes
      1. Getting ready
        1. Method application
        2. Signal description
      2. How to do it...
      3. How it works...
    6. Displaying two groups of checkboxes
      1. Getting ready
      2. How to do it...
      3. How it works...
  19. Event Handling - Signals and Slots
    1. Introduction
    2. Using Signal/Slot Editor
      1. How to do it...
    3. Copying and pasting text from one Line Edit widget to another
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Converting data types and making a small calculator
      1. How to do it...
      2. How it works...
    5. Using the Spin Box widget
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Using scrollbars and sliders
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Using List Widget
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Selecting multiple list items from one List Widget and displaying them in another
      1. How to do it...
      2. How it works...
    9. Adding items into List Widget
      1. How to do it...
      2. How it works...
    10. Performing operations in List Widget
      1. Getting ready
        1. Methods provided by the QListWidgetItem class
      2. How to do it....
      3. How it works...
    11. Using the Combo Box widget
      1. How to do it…
      2. How it works...
    12. Using the Font Combo Box widget
      1. Getting ready
      2. How to do it…
      3. How it works...
    13. Using the Progress Bar widget
      1. Getting ready
      2. How to do it…
      3. How it works...
  20. Understanding OOP Concepts
    1. Object-oriented programming
      1. Creating a class
      2. Using the built-in class attributes
        1. Accessing class variables in instance methods
          1. Instances
    2. Using classes in GUI
      1. How to do it...
      2. How it works...
        1. Making the application more elaborate
        2. Inheritance
          1. Types of inheritance
    3. Using single inheritance
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Using multilevel inheritance
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Using multiple inheritance
      1. Getting ready
      2. How to do it...
      3. How it works...
  21. Understanding Dialogs
    1. Introduction
    2. The input dialog box
    3. Using the input dialog 
      1. How to do it...
      2. How it works...
    4. Using the color dialog
      1. How to do it...
      2. How it works...
    5. Using the font dialog
      1. How to do it...
      2. How it works...
    6. Using the file dialog
      1. Getting ready
      2. How to do it...
      3. How it works...
  22. Understanding Layouts
    1. Understanding layouts
      1. Spacers
    2. Using Horizontal Layout
      1. How to do it...
      2. How it works...
    3. Using Vertical Layout
      1. How to do it...
      2. How it works...
    4. Using Grid Layout
      1. How to do it...
      2. How it works...
    5. Using Form Layout
      1. Getting ready
      2. How to do it...
      3. How it works...
  23. Networking and Managing Large Documents
    1. Introduction
    2. Creating a small browser
      1. How to do it...
      2. How it works...
    3. Creating a server-side application
      1. How to do it...
      2. How it works...
    4. Establishing client-server communication
      1. How to do it...
      2. How it works...
    5. Creating a dockable and floatable sign-in form
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Multiple Document Interface 
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Displaying information in sections using Tab Widget
      1. How to do it...
      2. How it works...
    8. Creating a custom menu bar
      1. How to do it…
      2. How it works...
  24. Database Handling
    1. Introduction
      1. Creating the cursor object
    2. Creating a database
      1. How to do it…
      2. How it works…
    3. Creating a database table
      1. How to do it…
      2. How it works…
    4. Inserting rows in the specified database table
      1. How to do it…
      2. How it works…
    5. Displaying rows in the specified database table
      1. How to do it…
      2. How it works…
    6. Navigating through the rows of the specified database table
      1. How to do it…
      2. How it works…
    7. Searching a database table for specific information
      1. How to do it…
      2. How it works…
    8. Creating a signin form – applying an authentication procedure
      1. How to do it…
      2. How it works…
    9. Updating a database table – changing a user's password 
      1. How to do it…
      2. How it works…
    10. Deleting a row from a database table
      1. How to do it…
      2. How it works…
  25. Using Graphics
    1. Introduction
    2. Displaying mouse coordinates
      1. How to do it...
      2. How it works...
    3. Displaying coordinates where the mouse button is clicked and released
      1. How to do it...
      2. How it works...
    4. Displaying a point where the mouse button is clicked
      1. How to do it...
      2. How it works...
    5. Drawing a line between two mouse clicks
      1. How to do it...
      2. How it works...
    6. Drawing lines of different types
      1. How to do it...
      2. How it works...
    7. Drawing a circle of a desired size
      1. How to do it...
      2. How it works...
    8. Drawing a rectangle between two mouse clicks
      1. How to do it...
      2. How it works...
    9. Drawing text in a desired font and size
      1. How to do it...
      2. How it works...
    10. Creating a toolbar that shows different graphics tools
      1. How to do it…
      2. How it works...
    11. Plotting a line using Matplotlib
      1. Getting ready
      2. How to do it...
      3. How it works...
    12. Plotting a bar using Matplotlib
      1. Getting ready
      2. How to do it...
      3. How it works...
  26. Implementing Animation
    1. Introduction
      1. Implementing animation
    2. Displaying a 2D graphical image
      1. How to do it...
      2. How it works...
    3. Making a ball move down on the click of a button
      1. How to do it...
      2. How it works...
    4. Making a bouncing ball
      1. How to do it...
      2. How it works...
    5. Making a ball animate as per the specified curve
      1. How to do it...
      2. How it works...
  27. Using Google Maps
    1. Introduction
    2. Finding out details of a location or a landmark
      1. How to do it…
      2. How it works…
    3. Getting complete information from latitude and longitude values
      1. How to do it…
      2. How it works…
    4. Finding out the distance between two locations
      1. How to do it…
      2. How it works…
    5. Displaying location on Google Maps 
      1. How to do it…
      2. How it works…
  28. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Python GUI Programming - A Complete Reference Guide
  • Author(s): Alan D. Moore, B. M. Harwani
  • Release date: June 2019
  • Publisher(s): Packt Publishing
  • ISBN: 9781838988470