Python GUI Programming with Tkinter

Book description

Find out how to create visually stunning and feature-rich applications by empowering Python's built-in Tkinter GUI toolkit

About This Book
  • Explore Tkinter's powerful features to easily design and customize your GUI application
  • Learn the basics of 2D and 3D animation in GUI applications.
  • Learn to integrate stunning Data Visualizations using Tkinter Canvas and Matplotlib.
Who This Book Is For

This book will appeal to developers and programmers who would like to build GUI-based applications. Knowledge of Python is a prerequisite.

What You Will Learn
  • Implement the tools provided by Tkinter to design beautiful GUIs
  • Discover cross-platform development through minor customizations in your existing application
  • Visualize graphs in real time as data comes in using Tkinter's animation capabilities
  • Use PostgreSQL authentication to ensure data security for your application
  • Write unit tests to avoid regressions when updating code
In Detail

Tkinter is a lightweight, portable, and easy-to-use graphical toolkit available in the Python Standard Library, widely used to build Python GUIs due to its simplicity and availability. This book teaches you to design and build graphical user interfaces that are functional, appealing, and user-friendly using the powerful combination of Python and Tkinter.

After being introduced to Tkinter, you will be guided step-by-step through the application development process. Over the course of the book, your application will evolve from a simple data-entry form to a complex data management and visualization tool while maintaining a clean and robust design. In addition to building the GUI, you'll learn how to connect to external databases and network resources, test your code to avoid errors, and maximize performance using asynchronous programming. You'll make the most of Tkinter's cross-platform availability by learning how to maintain compatibility, mimic platform-native look and feel, and build executables for deployment across popular computing platforms.

By the end of this book, you will have the skills and confidence to design and build powerful high-end GUI applications to solve real-world problems.

Style and approach

This is a comprehensive guide that explores the essential Tkinter features and modules and implements them in building real-world cross-platform GUI applications

Publisher resources

Download Example Code

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Python GUI Programming with Tkinter
  3. Dedication
  4. Packt Upsell
    1. Why subscribe?
  5. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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 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
  12. 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
  13. 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
  14. 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
  15. Maintaining Cross-Platform Compatibility
    1. Writing cross-platform Python
      1. Filenames and filepaths across platforms
        1. Path separators and drives
          1. Path separator translation
          2. The os.path module
          3. The pathlib module
        2. Case-sensitivity
        3. Symbolic links
        4. Path variables
      2. Inconsistent library and feature support
        1. Python's platform-limited libraries
        2. Checking low-level function compatibility
      3. The dangers of the subprocess module
      4. Text file encodings and formats
      5. Graphical and console modes
      6. Writing code that changes according to the platform
    2. Writing cross-platform Tkinter
      1. Tkinter version differences across platforms
      2. Application menus across platforms
        1. Menu widget capabilities
        2. Menu guidelines and standards
          1. Windows user experience interaction guidelines
          2. Apple's human interface guidelines
          3. Linux and BSD human interface guidelines
      3. Accelerator keys
      4. Fonts
      5. Theme support
      6. Window zoomed state
    3. Improving our application's cross-platform compatibility
      1. Storing preferences correctly
      2. Specifying an encoding for our CSV file
      3. Making platform-appropriate menus
        1. Preparing our MainMenu class
        2. Building the Windows menu
        3. Building the Linux menu
        4. Building the macOS menu
        5. Creating and using our selector function
    4. Summary
  16. 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
  17. 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
  18. 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 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
  19. Asynchronous Programming with Thread and Queue
    1. Tkinter's event queue
      1. The after() and after_idle() methods
        1. The update() and update_idletasks () methods
        2. Eliminating freezes with after() and update_idletasks ()
    2. Running code in the background with threading
      1. The threading module
      2. Converting our network functions to threads
        1. Simulating a slow server
        2. Demonstrating the threaded versus non-threaded uploader
    3. Passing messages using a queue
      1. The Queue object
      2. Using queues to communicate between threads
      3. Adding a communication queue to our threaded uploader
        1. Creating a communications protocol
        2. Sending messages from the uploader
        3. Handling queue messages
    4. Summary
  20. 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
  21. Packaging with setuptools and cx_Freeze
    1. Using setuptools
      1. Configuring a script
        1. Basic metadata arguments
        2. Packages and dependencies
        3. Adding extra files
        4. Defining commands
      2. Creating and using source distributions
        1. Testing our source distribution
      3. Building a wheel distribution
    2. Using cx_Freeze
      1. First steps with cx_Freeze
        1. The build_exe options
        2. Including external files
      2. Building executables
        1. Cleaning up the build
    3. Building Windows executables with cx_Freeze
      1. Building a Windows installer file
    4. Building macOS executables with cx_Freeze
      1. Building macOS application bundles
      2. Building macOS .dmg files
    5. Summary
  22. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Python GUI Programming with Tkinter
  • Author(s): Alan D. Moore
  • Release date: May 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788835886