Tkinter GUI Application Development Blueprints

Book description

Master GUI programming in Tkinter as you design, implement, and deliver ten real-world applications from start to finish

About This Book

  • Conceptualize and build state-of-art GUI applications with Tkinter
  • Tackle the complexity of just about any size GUI application with a structured and scalable approach
  • A project-based, practical guide to get hands-on into Tkinter GUI development

Who This Book Is For

Software developers, scientists, researchers, engineers, students, or programming hobbyists with basic familiarity in Python will find this book interesting and informative. People familiar with basic programming constructs in other programming language can also catch up with some brief reading on Python. No GUI programming experience is expected.

What You Will Learn

  • Get to know the basic concepts of GUI programming, such as Tkinter top-level widgets, geometry management, event handling, using callbacks, custom styling, and dialogs
  • Create apps that can be scaled in size or complexity without breaking down the core
  • Write your own GUI framework for maximum code reuse
  • Build apps using both procedural and OOP styles, understanding the strengths and limitations of both styles
  • Learn to structure and build large GUI applications based on Model-View-Controller (MVC) architecture
  • Build multithreaded and database-driven apps
  • Create apps that leverage resources from the network
  • Learn basics of 2D and 3D animation in GUI applications
  • Develop apps that can persist application data with object serialization and tools such as configparser

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, media player, drawing application, chat application, screen saver, port scanner, and many 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 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 upon the readers experience with Python.

Table of contents

  1. Tkinter GUI Application Development Blueprints
    1. Table of Contents
    2. Tkinter GUI Application Development Blueprints
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
    8. 1. Meet Tkinter
      1. Objectives of this chapter
      2. Installing Python and Tkinter
      3. Importing Tkinter
      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
        2. Passing arguments to callbacks
        3. Limitations of the command option
        4. Event binding
        5. Event patterns
        6. The levels of binding
      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
        1. Getting interactive help
      13. Summary
    9. 2. Making a Text Editor
      1. Objectives of the chapter
      2. An overview of the chapter
      3. Setting up the editor skeleton
      4. Adding a menu and menu items
        1. Adding menu items
      5. Implementing the View menu
      6. Adding a built-in functionality
      7. Indexing and tagging
        1. Index
        2. Tags
      8. Implementing the Select All feature
      9. Implementing the Find Text feature
      10. Types of Toplevel windows
      11. Working with forms and dialogs
      12. Working with message boxes
      13. The icons toolbar and View menu functions
      14. Displaying the line number
      15. Adding the cursor information bar
      16. Adding themes
      17. Creating the context/pop-up menu
      18. Summary
    10. 3. Programmable Drum Machine
      1. An overview of the chapter
      2. Module requirements for the chapter
      3. Setting up the GUI in OOP
      4. Finalizing the data structure
      5. Creating broader visual elements
        1. Defining getter and setter methods
        2. The number of units and beats per unit features
      6. Loading drum samples
      7. Playing the drum machine
        1. Initializing pygame
        2. Playing complete patterns
        3. Determining the tempo of a rhythm
      8. Tkinter and threading
        1. Tkinter and thread safety
      9. Support for multiple beat patterns
      10. Saving beat patterns
      11. Working with ttk-themed widgets
      12. Summary
    11. 4. A Game of Chess
      1. An overview of the chapter
        1. Module requirements for this chapter
      2. Structuring our program
      3. Modeling the data structures
        1. Convention on naming chess pieces
        2. Convention for naming locations on the chessboard
      4. Creating a Piece class
        1. Displaying chess pieces on the chessboard
        2. Defining rules for the chess pieces
        3. Rules for the King, Queen, Rook, and Bishop
        4. Rules for a Knight
        5. Rules for a Pawn
        6. The movement validation of chess pieces
        7. Tracking all the available moves
        8. Finding out the current position of the King
          1. Checking whether the King is in check
      5. 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
        5. Recording a move in the data structure
          1. Keep game statistics
      6. Managing user preferences
      7. Summary
    12. 5. 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
    13. 6. Paint Application
      1. Creating a tiny framework
      2. Setting up a broad GUI structure
      3. Dealing with mouse events
      4. Adding toolbar buttons
      5. Drawing items on the canvas
      6. Adding a color palette
      7. Adding top bar options for draw methods
      8. Drawing irregular lines and super shapes
        1. Drawing irregular lines
        2. Drawing super shapes
      9. Adding functionality to the remaining buttons
      10. Adding functionality to menu items
      11. Summary
    14. 7. Multiple Fun Projects
      1. Chapter objectives
      2. Building a screen saver
      3. Building a Snake game
      4. Creating a Weather Reporter
        1. A simple socket demo
        2. Building a port scanner
        3. Building a chat application
      5. Creating a phone book application
        1. Creating a new record
        2. Reading from the database
        3. Updating records
        4. Deleting records
      6. Graphing with Tkinter
      7. 3D graphics with Tkinter
      8. Summary
    15. 8. Miscellaneous Tips
      1. Chapter objectives
      2. Tracing Tkinter variables
      3. Widget traversal
      4. Validating user input
        1. Key validation mode demo
        2. Focus out validation mode demo
      5. Formatting widget data
      6. More on fonts
        1. Finer control over font
        2. Building a font selector
      7. Redirecting the command-line output to Tkinter
      8. The class hierarchy of Tkinter
      9. 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
      10. Tips for program optimization
        1. Using filter and map
        2. Optimize 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
        6. Alternatives to Tkinter
        7. wxPython
        8. PyQt
        9. PySide
        10. PyGTK
        11. Other options
      13. Tkinter in Python 2.x
      14. Summary
    16. Index

Product information

  • Title: Tkinter GUI Application Development Blueprints
  • Author(s): Bhaskar Chaudhary
  • Release date: November 2015
  • Publisher(s): Packt Publishing
  • ISBN: 9781785889738