0%

Book Description

Tkinter is a great place to start with GUI Application development and this book is designed to make the process fun and informative. With bags of real examples, like a drum machine and chess game, it will get you coding in no time.

  • Develop 10 real GUI applications using procedural and object-oriented styles
  • Write network programs, database-driven programs, multi-threaded programs and more with Tkinter
  • A practical guide to discover the best practices involved in writing GUI applications

In Detail

Tkinter is the built-in GUI package that comes with standard python distributions. This means it is easy to get started right away, without any extra installation or configuration. Tkinter’s strength lies in its simplicity of use and its intuitive nature which makes it suited for programmers and non-programmers alike. Once you get started, you will be surprised to see how a few lines of code can produce powerful GUI applications.

Tkinter GUI Application Development Hotshot helps you learn the art of GUI programming—building real-world, productive and fun applications like text editor, drum machine, game of chess, media player, drawing application and many more. Each subsequent project builds on the skills acquired in the previous project. Also, learn to write multi-threaded and multi layered applications using Tkinter. Get to know modern best practices involved in writing GUI programs. Tkinter GUI Application Development Hotshot comes with a rich source of sample codes that you can use in your own projects in any discipline of your choice.

Starting with a high level overview of Tkinter that covers the most important concepts involved in writing a GUI application, the book then takes you through a series of real world projects of increasing complexity, developing one project per chapter. After you have developed five full projects, the book provides you with some bare-bone skeleton codes for a few functional but incomplete projects, challenging you to put your skills to test by completing them.

Finally, you are provided with tips for writing reusable, scalable, and quality GUI code for larger projects. The appendices provide a quick reference sheet for Tkinter.

Table of Contents

  1. Tkinter GUI Application Development HOTSHOT
    1. Table of Contents
    2. Tkinter GUI Application Development HOTSHOT
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
      2. Why Subscribe?
      3. 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. Errata
        3. Piracy
        4. Questions
    8. 1. Meet Tkinter
      1. Mission Briefing
        1. Why Is It Awesome?
        2. Your Hotshot Objectives
        3. Mission Checklist
      2. The root window – your drawing board
        1. Engage Thrusters
        2. Objective Complete – Mini Debriefing
        3. Classified Intel
          1. The big picture
      3. Widgets – building blocks for your GUI program
        1. Engage Thrusters
        2. Objective Complete – Mini Debriefing
        3. Classified Intel
      4. Getting to know core Tkinter widgets
        1. Prepare for Lift Off
        2. Engage Thrusters
        3. Objective Complete – Mini Debriefing
        4. Classified Intel
      5. Geometry management
        1. Prepare for Lift Off
        2. Engage Thrusters
          1. The pack geometry manager
          2. The grid geometry manager
          3. The place geometry manager
        3. Objective Complete – Mini Debriefing
      6. Events and callbacks – adding life to programs
        1. Engage Thrusters
          1. Command binding
          2. Passing arguments to the callback
          3. Event binding
          4. Event pattern
          5. Levels of binding
          6. Handling widget-specific variables
        2. Objective Complete – Mini Debriefing
        3. Classified Intel
      7. Doing it in style
        1. Prepare for Lift Off
        2. Engage Thrusters
        3. Objective Complete – Mini Debriefing
      8. Mission Accomplished
      9. A Hotshot Challenge
    9. 2. Making a Text Editor like Notepad
      1. Mission Briefing
        1. Why Is It Awesome?
        2. Your Hotshot Objectives
      2. Setting up the widgets
        1. Prepare for Lift Off
        2. Engage Thrusters
        3. Objective Complete – Mini Debriefing
      3. Leveraging the power of built-in Text widget options
        1. Engage Thrusters
        2. Objective Complete – Mini Briefing
      4. Indexing and tagging
        1. Prepare for Lift Off
          1. Index
          2. Tags
        2. Engage Thrusters
        3. Objective Complete – Mini Briefing
        4. Classified Intel
      5. Working with forms and dialogs
        1. Prepare for Lift Off
        2. Engage Thrusters
        3. Objective Complete – Mini Briefing
      6. Working with message boxes
        1. Prepare for Lift Off
        2. Engage Thrusters
        3. Objective Complete – Mini Briefing
      7. The icon toolbar and View menu functions
        1. Engage Thrusters
        2. Objective Complete – Mini Briefing
      8. Event handling and the context menu
        1. Engage Thrusters
        2. Objective Complete – Mini Briefing
      9. Mission Accomplished
      10. A Hotshot Challenge
    10. 3. Programmable Drum Machine
      1. Mission Briefing
        1. Why Is It Awesome?
        2. Your Hotshot Objectives
        3. Mission Checklist
      2. Setting up the GUI in OOP
        1. Prepare for Lift Off
        2. Engage Thrusters
        3. Objective Complete – Mini Debriefing
      3. Completing the pattern editor
        1. Engage Thrusters
        2. Objective Complete – Mini Debriefing
      4. Loading drum samples
        1. Engage Thrusters
        2. Objective Complete – Mini Debriefing
      5. Playing the drum machine
        1. Prepare for Lift Off
        2. Engage Thrusters
        3. Objective Complete – Mini Debriefing
        4. Classified Intel
      6. Tkinter and threading
        1. Prepare for Lift Off
        2. Engage Thrusters
        3. Objective Complete – Mini Debriefing
      7. More beat patterns
        1. Engage Thrusters
        2. Objective Complete – Mini Debriefing
      8. Object persistence
        1. Prepare for Lift Off
        2. Engage Thrusters
        3. Objective Complete – Mini Debriefing
      9. ttk-themed widgets
        1. Prepare for Lift Off
        2. Engage Thrusters
        3. Objective Complete – Mini Debriefing
      10. Mission Accomplished
      11. A Hotshot Challenge
    11. 4. Game of Chess
      1. Mission Briefing
        1. Why Is It Awesome?
        2. Your Hotshot Objectives
        3. Mission Checklist
      2. Structuring our program
        1. Prepare for Lift Off
        2. Engage Thrusters
          1. Step 1 – creating the GUI class
          2. Step 2 – creating the chessboard
          3. Step 3 – creating Tkinter mainloop
        3. Objective Complete – Mini Debriefing
        4. Classified Intel
      3. Structuring chessboard-and-pieces-related data
        1. Prepare for Lift Off
        2. Engage Thrusters
          1. Step 1 – creating a Piece superclass
          2. Step 2 – creating individual child classes for all pieces
          3. Step 3 – defining a method to return the piece instance
          4. Step 4 – creating the Board class
          5. Step 5 – displaying pieces on board for a given FEN notation
          6. Step 6 – checking if a given coordinate is on the board
          7. Step 7 – generating alphabetic and numeric notation
          8. Step 8 – checking places occupied on the board
          9. Step 9 – handling errors and exceptions
        3. Objective Complete – Mini Debriefing
      4. Adding pieces on the board
        1. Prepare for Lift Off
        2. Engage Thrusters
          1. Step 1 – importing PIL
          2. Step 2 – defining method to draw pieces on chessboard
        3. Objective Complete – Mini Debriefing
      5. Enforcing rules for pieces' movement
        1. Prepare for Lift Off
        2. Engage Thrusters
          1. Step 1 – tracking moves available for all pieces from Pieces superclass
          2. Step 2 – rules for the king, queen, rook and bishop class
          3. Step 3 – rules for knight
          4. Step 4 – rules for pawn
        3. Objective Complete – Mini Debriefing
      6. The chessboard logic
        1. Engage Thrusters
          1. Step 1: Tracking all Available Moves
          2. Step 2: Getting Current Position of King
          3. Step 3: Verifying if King is under Check
        2. Objective Complete – Mini Debriefing
      7. Making the chess functional
        1. Engage Thrusters
          1. Step 1 – updating the board for change in FEN notation
          2. Step 2 – binding mouse click event
          3. Step 3 – adding attribute to track selected piece and remaining pieces
          4. Step 4 – identifying square clicked
          5. Step 5 – getting the source and destination position
          6. Step 6 – collecting list of moves to highlight
          7. Step 7 – modifying draw_board to highlight allowed moves
          8. Step 8 – defining attributes to keep game statistics
          9. Step 9 – preshift validations
          10. Step 10 – actual movement of pieces
          11. Step 11 – Post movement updates
          12. Step 12 – classes to handle exceptions and errors
        2. Objective Complete – Mini Debriefing
      8. Adding menu and an info frame
        1. Engage Thrusters
          1. Step 1 – creating top menu
          2. Step 2 – adding the bottom frame to display game statistics
          3. Step 3 – starting a new game from File | New game menu
          4. Step 4 – updating bottom label after every move
        2. Objective Complete – Mini Debriefing
      9. Mission Accomplished
      10. A Hotshot Challenge
    12. 5. Audio Player
      1. Mission Briefing
        1. Why Is It Awesome?
        2. Your Hotshot Objectives
        3. Mission Checklist
          1. Pyglet for audio manipulation
          2. PMW Tkinter extension
          3. Additional font
      2. Getting the audio to play
        1. Prepare for Lift Off
        2. Engage Thrusters
          1. Step 1 – creating the GUI class
          2. Step 2 – creating the Play button and Add File button
          3. Step 3 – toggling between play and pause
          4. Step 4 – add file dialog
          5. Step 5 – creating the Player class
          6. Step 6 – running the application
        3. Objective Complete – Mini Debriefing
      3. Adding a playlist
        1. Prepare for Lift Off
        2. Engage Thrusters
          1. Step 1 – adding an empty Listbox widget
          2. Step 2 – identify track to play
          3. Step 3 – adding items to the list
          4. Step 4 – deleting items from the list
          5. Step 5 – adding multiple items to the list
          6. Step 6 – deleting all items
        3. Objective Complete – Mini Debriefing
      4. Adding more controls to the player
        1. Engage Thrusters
          1. Step 1 – fast forwarding a track
          2. Step 2 – rewinding a track
          3. Step 3 – pausing a track
          4. Step 4 – setting the volume of playback
          5. Step 5 – muting and unmuting a track
          6. Step 6 – adding the control buttons
          7. Step 7 – changing volume with the ttk Scale widget
        2. Objective Complete – Mini Debriefing
        3. Classified Intel
      5. Adding the top display console
        1. Prepare for Lift Off
        2. Engage Thrusters
          1. Step 1 – creating the top console and progress bar
          2. Step 2 – getting the total duration of a track
          3. Step 3 – updating console at launch of play
          4. Step 4 – updating timer clock and progress bar at regular intervals
        3. Objective Complete – Mini Debriefing
      6. Looping over tracks
        1. Engage Thrusters
          1. Step 1 – creating the radio buttons
          2. Step 2 – on end of song callback
          3. Step 3 – what next?
          4. Step 4 – fetching the next track
        2. Objective Complete – Mini Debriefing
      7. Adding the contextual menu
        1. Engage Thrusters
          1. Step 1 – creating the contextual menu
          2. Step 2: overriding the close button
        2. Objective Complete – Mini Debriefing
      8. Adding a tooltip and finalizing our player
        1. Prepare for Lift Off
          1. PMW list of extensions
            1. Widgets
            2. Dialogs
            3. Miscellaneous
        2. Engage Thrusters
          1. Step 1 – importing PMW
          2. Step 2 – instantiating the Balloon widget
          3. Step 3 – adding Balloon tooltips to all buttons in our player
        3. Objective Complete – Mini Debriefing
      9. Mission Accomplished
      10. A Hotshot Challenge
    13. 6. Drawing an Application
      1. Mission Briefing
        1. Why Is It Awesome?
        2. Your Hotshot Objectives
        3. Mission Checklist
      2. Developing a bare bone GUI framework
        1. Prepare for Lift Off
        2. Engage Thrusters
          1. Step 1 – creating the GUI framework class
          2. Step 2 – creating menu builder
          3. Step 3 – testing our new framework
        3. Objective Complete – Mini Debriefing
        4. Classified Intel
      3. Structuring our drawing program
        1. Engage Thrusters
          1. Step 1 – importing framework
          2. Step 2: building the top menu
          3. Step 3 – undo operation on the Canvas widget
          4. Step 4 – saving canvas objects
          5. Step 5 – creating buttons in the left tool bar
          6. Step 6 – adding command callback to buttons
          7. Step 7 – creating color palettes and color selection dialog
          8. Step 8 – setting color of background and foreground palette
          9. Step 9 – displaying x and y coordinates of mouse movement
        2. Objective Complete – Mini Debriefing
      4. Handling mouse events
        1. Engage Thrusters
          1. Step 1 – binding mouse down, mouse motion, and mouse release over the canvas
          2. Step 2 – calculating coordinates of mouse movement
          3. Step 3 – updating the current mouse position label in the left tool bar
        2. Objective Complete – Mini Debriefing
      5. Drawing items on the canvas
        1. Engage Thrusters
          1. Step 1 – creating a tuple of methods
          2. Step 2 – add icons to our toolbar buttons
          3. Step 3 – keeping a tab on currently selected button
          4. Step 4 – code for drawing line, rectangle, and oval shapes
          5. Step 5 – code for drawing in continuous stroke
          6. Step 6 – executing code dynamically
          7. Step 7 – doing the actual drawing
        2. Objective Complete – Mini Debriefing
      6. Setting the options toolbar at the top
        1. Engage Thrusters
          1. Step 1 – showing the selected button icon at the top
          2. Step 2 – adding the Combobox widget to let user select different fill options
          3. Step 3 – modifying draw methods to add configurable options
        2. Objective Complete – Mini Debriefing
      7. Adding some more features
        1. Engage Thrusters
          1. Step 1 – extending our methods tuple
          2. Step 2 – targeting a given item on the canvas
          3. Step 3 – deleting items from the canvas
          4. Step 4 – paint bucket feature
          5. Step 5 – moving items on top of each other
          6. Step 6 – dragging items on the canvas
        2. Objective Complete – Mini Debriefing
        3. Classified Intel
          1. Working with item tags
          2. Adding a tag
          3. Retrieving tags
          4. Getting items with a given tag
          5. Built-in tags
      8. Mission Accomplished
      9. A Hotshot Challenge
    14. 7. Some Fun Project Ideas
      1. Mission Briefing
        1. Why Is It Awesome?
        2. Your Hotshot Objectives
      2. Building a screen saver
        1. Engage Thrusters
        2. Objective Complete – Mini Debriefing
      3. Building a Snake game
        1. Prepare for Lift Off
        2. Engage Thrusters
        3. Objective Complete – Mini Debriefing
      4. Creating a Weather Reporter
        1. Prepare for Lift Off
        2. Engage Thrusters
        3. Objective Complete – Mini Debriefing
        4. Classified Intel
      5. Creating a phonebook application
        1. Prepare for Lift Off
        2. Engage Thrusters
        3. Objective Complete – Mini Debriefing
      6. Graphing with Tkinter
        1. Engage Thrusters
        2. Objective Complete – Mini Debriefing
      7. Mission Accomplished
      8. A Hotshot Challenge
    15. A. Miscellaneous Tips
      1. Mission Briefing
      2. Tracing Tkinter variables
      3. Widget traversal
      4. Validating user input
        1. Key Validation
        2. Focus Out Validation
      5. Formatting widget data
      6. More on fonts
        1. Finer Control over Font
        2. Font Selector
        3. Creating the GUI
        4. Updating Sample Text
      7. Working with Unicode characters
        1. Declaring line encoding
        2. Declaring file encoding
      8. Tkinter class's hierarchy
      9. Custom-made mixins
      10. Tips for code cleanup and program optimization
        1. Choose the right data structure
        2. Working with Variables
        3. Using Exceptions
        4. Filter and map
        5. Profiling
        6. Other Optimization Tips
      11. Distributing the Tkinter application
        1. py2exe
        2. py2app
        3. PyInstaller
        4. Other Freezing Tools
      12. Limitations of Tkinter
        1. Limited number of core widgets
        2. No Support for printing
        3. No support for newer image formats
        4. Inactive development community
      13. Alternatives to Tkinter
        1. wxPython
        2. PyQt
        3. PySide
        4. PyGTK
        5. Other Options
      14. Getting interactive help
      15. Tkinter in Python 3.x
      16. Conclusion
    16. B. Quick Reference Sheets
      1. Options common to widgets
      2. Widget-specific options
      3. The pack manager
      4. The grid manager
      5. The place manager
      6. The event types
      7. The event modifiers
      8. The event details
      9. Other event-related methods
      10. List of available cursor
        1. Potability issues
      11. The basic widget methods
      12. ttk widgets
      13. The Toplevel window methods
    17. Index
3.137.212.124