O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Tkinter GUI Application Development Blueprints - Second Edition

Book Description

Geometry Management, Event Handling, and more

About This Book

  • A Practical, guide to learn the application of Python and GUI programming with tkinter
  • Create multiple cross-platform real-world projects by integrating host of third party libraries and tools
  • Learn to build beautiful and highly interactive user interfaces, targeting multiple devices.

Who This Book Is For

This book is for a beginner to intermediate-level Pythonists who want to build modern, cross-platform GUI applications with the amazingly powerful Tkinter. Prior knowledge of Tkinter is required.

What You Will Learn

  • A Practical, guide to help you learn the application of Python and GUI programming with Tkinter
  • Create multiple, cross-platform, real-world projects by integrating a host of third-party libraries and tools
  • Learn to build beautiful and highly interactive user interfaces, targeting multiple devices.

In Detail

Tkinter is the built-in GUI package that comes with standard Python distributions. It is a cross-platform package, which means you build once and deploy everywhere. It is simple to use and intuitive in nature, making it suitable for programmers and non-programmers alike.

This book will help you master the art of GUI programming. It delivers the bigger picture of GUI programming by building real-world, productive, and fun applications such as a text editor, drum machine, game of chess, audio player, drawing application, piano tutor, chat application, screen saver, port scanner, and much more. In every project, you will build on the skills acquired in the previous project and gain more expertise. You will learn to write multithreaded programs, network programs, database-driven programs, asyncio based programming and more. You will also get to know the modern best practices involved in writing GUI apps.

With its rich source of sample code, you can build upon the knowledge gained with this book and use it in your own projects in the discipline of your choice.

Style and approach

An easy-to-follow guide, full of hands-on examples of real-world GUI programs. The first chapter is a must-read as it explains most of the things you need to get started with writing GUI programs with Tkinter. Each subsequent chapter is a stand-alone project that discusses some aspects of GUI programming in detail. These chapters can be read sequentially or randomly, depending on the reader's experience with Python.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Tkinter GUI Application Development Blueprints Second Edition
  3. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  4. Contributors
    1. About the author
    2. About the reviewer
    3. 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. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  6. Meet Tkinter
    1. Technical requirements
    2. Project overview
    3. Getting started
    4. GUI programming – the big picture
    5. The root window – your drawing board
    6. Widgets – the building blocks of GUI programs
      1. Some important widget features
      2. Ways to create widgets
      3. Getting to know the core Tkinter widgets
      4. Adding widgets to a parent window
    7. The Tkinter geometry manager
      1. The pack geometry manager
      2. The grid geometry manager
      3. The place geometry manager
    8. Events and callbacks – adding life to programs
      1. Command binding
        1. Passing arguments to callbacks
        2. Limitations of the command option
      2. Event binding
      3. Event patterns
      4. Binding levels
    9. Handling widget-specific variables
    10. Event unbinding and virtual events
    11. Doing it in style
      1. Specifying styles
    12. Some common root window options
    13. Getting interactive help
    14. Summary
    15. QA section
    16. Further reading
  7. Making a Text Editor
    1. Project overview
    2. Getting started – setting up the editor skeleton
    3. Adding a menu and menu items
      1. Adding menu items
    4. Implementing the View menu
    5. Adding a built-in functionality
    6. Indexing and tagging
      1. Index
      2. Tags
    7. Implementing the Select All feature
    8. Implementing the Find Text feature
    9. Types of Toplevel window
    10. Working with forms and dialogs
    11. Working with message boxes
    12. The icons toolbar and View menu functions
    13. Displaying line numbers
    14. Adding the cursor information bar
    15. Adding themes
    16. Creating a context/pop-up menu
    17. Summary
    18. QA section
    19. Further reading
  8. Programmable Drum Machine
    1. Getting started
    2. Technical requirements
    3. Setting up the GUI in OOP
    4. Finalizing the data structure
    5. Creating broader visual elements
    6. Defining getter and setter methods
    7. The number of units and beats per unit features
    8. Loading drum samples
    9. Playing the drum machine
      1. Initializing pygame
      2. Playing complete patterns
      3. Determining the tempo of a rhythm
    10. Tkinter and threading
      1. Tkinter and thread safety
    11. Support for multiple beat patterns
    12. Saving beat patterns
    13. Working with ttk-themed widgets
    14. Summary
    15. QA section
    16. Further reading
  9. Game of Chess
    1. An overview of the chapter
    2. Module requirements for this chapter
    3. Structuring our program
    4. Modeling the data structure
      1. Convention on naming chess pieces
      2. Convention for naming locations on the chessboard
    5. Creating a Piece class
      1. Displaying chess pieces on the chessboard
      2. Defining rules for the chess pieces
      3. Rules for the king, queen, rooks, and bishops
      4. Rules for the Knight
      5. Rules for a pawn
      6. Movement validation of chess pieces
      7. Tracking all available moves
      8. Finding out the current position of the king
        1. Checking whether the king is in check
    6. Making the game functional
      1. Getting the source and destination position
      2. Collecting a list of the moves that need to be highlighted
      3. Highlighting allowed moves
      4. Pre-move validation
        1. Check whether a move will cause check on the King
      5. Recording a move in the data structure
        1. Keep game statistics
    7. Managing user preferences
    8. Summary
    9. QA section
    10. Further reading
  10. Building an Audio Player
    1. An overview of the chapter
    2. External library requirements
      1. The pyglet module
      2. Pmw Tkinter extension
    3. Program structure and broadview skeleton
    4. Deciding the data structure
    5. Creating the Player class
    6. Adding and removing items from a playlist
      1. Adding a single audio file
      2. Removing the selected files from a playlist
      3. Adding all files from a directory
      4. Emptying the playlist
    7. Playing audio and adding audio controls
      1. Adding the play/stop function
      2. Adding the pause/unpause function
      3. Adding the mute/unmute function
      4. Fast forward/rewind function
      5. Adding the next track/previous track function
      6. Adding the volume change function
    8. Creating a seek bar
    9. One-time updates during audio playback
    10. Managing continuous updates
    11. Looping over tracks
    12. Adding a tooltip
      1. Pmw list of extensions
        1. Widgets
        2. Dialogs
        3. Miscellaneous
    13. Summary
    14. QA section
    15. Further reading
  11. Paint Application
    1. Overview of the application
    2. Creating a tiny framework
    3. Setting up a broad GUI structure
    4. Dealing with mouse events
    5. Adding toolbar buttons
    6. Drawing items on the canvas
    7. Adding a color palette
    8. Adding top bar options for draw methods
    9. Drawing irregular lines and super shapes
      1. Drawing irregular lines
      2. Drawing super shapes
    10. Adding functionality to the remaining buttons
    11. Adding functionality to menu items
    12. Summary
    13. QA section
    14. Further reading
  12. Piano Tutor
    1. Technical requirements
    2. A brief primer on piano terms
      1. Learning about scales
      2. Learning about chords
    3. Building the broad GUI structure
      1. Putting up the skeleton structure
    4. Making the piano keyboard
      1. Putting the keyboard together
    5. Playing audio
    6. Building the scales tutor
    7. Building the chord finder section
    8. Building the chord progression tutor
    9. Building the score maker
    10. A note on window responsiveness
      1. Experimenting with the code
      2. Handling widget resize with <Configure>
    11. Summary
    12. QA section
    13. Further reading
  13. Fun with Canvas
    1. Building a screen saver
    2. Graphing with Tkinter
      1. Polar plots with Tkinter
    3. Gravity simulation
    4. Drawing fractals
    5. Voronoi diagrams
    6. Spring pendulum simulation
    7. Chaos game – building triangles out of randomness
    8. Phyllotaxy
    9. 3D graphics with Tkinter
    10. Summary
    11. QA section
    12. Further reading
  14. Multiple Fun Projects
    1. Technical requirements
    2. Building a Snake game
      1. Understanding a race condition
      2. Using synchronization primitives
      3. Using queues
      4. Building the Snake game
        1. The View class
        2. The Food class
        3. The Snake class
        4. Queue handler
    3. Creating a Weather Reporter application
    4. A simple socket demo
    5. Building a port scanner
    6. Building a chat application
    7. Creating a phone book application
      1. Creating a new record
      2. Reading from the database
      3. Updating records
      4. Deleting records
    8. Using asyncio with Tkinter
    9. Interfacing with hardware/serial communication
      1. Hardware
      2. Writing the Arduino sketch
      3. Reading serial data
    10. Summary
    11. QA section
    12. Further reading
  15. Miscellaneous Tips
    1. Tracing Tkinter variables
    2. Widget traversal
    3. Validating user input
      1. Key validation mode demo
      2. Focus-out validation mode demo
    4. Formatting widget data
    5. More on fonts
      1. Finer control over font
      2. Building a font selector
    6. Redirecting the command-line output to Tkinter
    7. The class hierarchy of Tkinter
    8. Tips for program design
      1. The model-first policy versus the code-first policy
      2. Separating the model from the view
      3. Selecting the right data structure
      4. Naming variables and methods
      5. The Single Responsibility Principle
      6. Loose coupling
      7. Handling errors and exceptions
    9. Handling cross-platform differences 
    10. Tips for program optimization
      1. Using filter and map
      2. Optimizing variables
      3. Profiling your program
      4. Other optimization tips
    11. Distributing a Tkinter application
      1. py2exe
      2. py2app
      3. PyInstaller
      4. Other freezing tools
    12. The limitations of Tkinter
      1. A limited number of core widgets
      2. Non-Python objects
      3. No support for printing
      4. No support for newer image formats
      5. Inactive development community
    13. Alternatives to Tkinter
      1. wxPython
      2. PyQt
      3. PySide
      4. PyGTK
      5. Other options
    14. Tkinter in Python 2.x
    15. Summary
    16. QA section
  16. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think