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 Programming by Example

Book Description

Leverage the power of Python and its de facto GUI framework to build highly interactive interfaces

About This Book
  • The fundamentals of Python and GUI programming with Tkinter.
  • Create multiple cross-platform projects by integrating a host of third-party libraries and tools.
  • Build beautiful and highly-interactive user interfaces that target multiple devices.
Who This Book Is For

This book is for beginners to GUI programming who haven't used Tkinter yet and are eager to start building great-looking and user-friendly GUIs. Prior knowledge of Python programming is expected.

What You Will Learn

  • Create a scrollable frame via theCanvas widget
  • Use the pack geometry manager andFrame widget to control layout
  • Learn to choose a data structurefor a game
  • Group Tkinter widgets, such asbuttons, canvases, and labels
  • Create a highly customizablePython editor
  • Design and lay out a chat window
In Detail

Tkinter is a modular, cross-platform application development toolkit for Python. When developing GUI-rich applications, the most important choices are which programming language(s) and which GUI framework to use. Python and Tkinter prove to be a great combination. This book will get you familiar with Tkinter by having you create fun and interactive projects. These projects have varying degrees of complexity. We'll start with a simple project, where you'll learn the fundamentals of GUI programming and the basics of working with a Tkinter application. After getting the basics right, we'll move on to creating a project of slightly increased complexity, such as a highly customizable Python editor. In the next project, we'll crank up the complexity level to create an instant messaging app. Toward the end, we'll discuss various ways of packaging our applications so that they can be shared and installed on other machines without the user having to learn how to install and run Python programs.

Style and approach

Step by Step guide with real world examples

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Tkinter GUI Programming by Example
  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. Installation
    2. How will the code be structured?
      1. Geometry managers
        1. pack
        2. grid
        3. place
          1. To pack or to grid?
    3. Getting going
    4. Adding interactivity
    5. Using variables
      1. Our first try
      2. Creating Tkinter-compatible variables
        1. Using and updating
      3. Fixing our application
    6. Showing messages
      1. Showing information with showinfo
      2. Showing warnings or errors
      3. Getting feedback from the user
    7. Getting text input
    8. Summary
  7. Back to the Command Line – Basic Blackjack
    1. Python's class system
      1. Instances
      2. Inheritance
    2. Blackjack's classes
      1. The Card class
      2. The Deck class
      3. The Hand class
    3. The Game class and main loop
    4. Command line versus GUI
      1. Interactivity
      2. Familiarity
      3. Ease of use
      4. Size and portability
    5. Summary
  8. Jack is Back in Style – the Blackjack GUI
    1. Moving from the command line to a graphical interface
    2. The Canvas widget
    3. Creating a graphical blackjack game
      1. Card, Deck, and Hand
      2. The GameState class
      3. The GameScreen class
    4. Playing our game
    5. Summary
  9. The Finishing Touches – Sound and Animation
    1. Python's module system
    2. The blackjack packages
      1. The casino package
      2. The casino_sounds package
        1. Setting up a virtual environment
        2. Creating the package
    3. The blackjack.py file
      1. Initializing the GameWindow class
      2. The GameScreen class
      3. The GameState class
      4. Choosing to hit
      5. Choosing to stick
      6. Running out of money
      7. Finishing off
    4. Summary
  10. Creating a Highly Customizable Python Editor
    1. The ttk submodule
      1. Styling a tk widget
      2. Styling a ttk widget
      3. Ttk style inheritance
    2. Beginning our text editor
    3. Tkinter's event system
      1. Binding an event
      2. Overwriting default events
      3. Generating events
    4. Events in our text editor
    5. A second top-level window
    6. Summary
  11. Color Me Impressed! – Adding Syntax Highlighting
    1. Tkinter's indexing system
      1. Getting the cursor's position
      2. Named indexes
      3. Special strings
        1. Line endings
        2. Horizontal movement
        3. Vertical movement
        4. Line beginning and end
        5. Word beginning and end
      4. Expanding our demo
    2. Using tags
    3. Searching text
    4. Adding syntax highlighting to our text editor
      1. The Highlighter class
      2. Using our Highlighter class
      3. The LineNumbers class
      4. Using our LineNumbers class
      5. Integrating our FindWindow class
      6. Using our FindWindow class
    5. Summary
  12. Not Just for Restaurants – All About Menus
    1. The Menu widget
      1. A menu bar
      2. A floating menu
    2. Adding a menu bar to our text editor
    3. Adding a context menu to our text editor
    4. Handling files
    5. Changing the syntax highlighting
    6. Changing the editor's font
      1. The Listbox widget
      2. The Spinbox widget
      3. Saving the user's choices
    7. Changing the editor's color scheme
    8. Summary
  13. Talk Python to Me – a Chat Application
    1. Creating a scrollable frame
    2. Creating our FriendsList class
    3. Creating our ChatWindow class
    4. Creating our SmilieSelect class
    5. Summary
  14. Connecting – Getting Our Chat Client Online
    1. Introduction to flask
      1. Our first web page
      2. Using JSON
    2. The requests module
      1. Sending a GET request
      2. Sending a POST request
    3. The sqlite3 module
      1. Creating a database and table
      2. Adding data to a SQLite database
      3. Selecting data from a SQLite database
    4. Linking flask and sqlite
    5. Updating our FriendsList class
    6. Creating the Requester class
    7. Connecting our FriendsList to our web service
    8. Connecting our ChatWindow
      1. Updating our server to store conversations
        1. Creating the Conversation class
        2. Using the Conversation class in our server
        3. Adding the new endpoints to our Requester
      2. Updating our ChatWindow class to send requests to the server
    9. Summary
  15. Making Friends – Finishing Our Chat Application
    1. Using threads
      1. Why use a thread with a GUI application?
      2. Using a thread
    2. Adding a Thread to our ChatWindow
      1. Creating new endpoints
      2. The ListeningThread class
      3. Implementing the ListeningThread class in our ChatWindow
    3. Allowing users to upload avatars
      1. The AvatarWindow class
      2. Adjusting the database
      3. Adding server endpoints
      4. Updating the FriendsList class
      5. Manipulating images with PIL
    4. Adding and blocking other users
      1. New database table
      2. Creating the server endpoints
      3. Tying it all together
    5. Summary
  16. Wrapping Up – Packaging Our Applications to Share
    1. Unexplored widgets
      1. The LabelFrame widget
      2. The Checkbutton and Radiobutton widgets
      3. The OptionMenu and Combobox widgets
      4. The Notebook widget
    2. Packaging applications
      1. Adjusting our text editor for portability
      2. Preparing to package with setup.py
      3. Installing our text editor
        1. Cross-platform using Pip
        2. Windows
        3. Linux
        4. macOS
    3. Summary
  17. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think