Book Image

Tkinter GUI Programming by Example

Book Image

Tkinter GUI Programming by Example

Overview of this book

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.
Table of Contents (18 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
Index

Preface

Welcome to Tkinter GUI Programming by Example. We will be exploring how to use the Tkinter library, which is included alongside most Python installs. This framework is very simple to use, and has a powerful event-handling system and an open license, perfect for anyone who wishes to quickly write and share graphical applications.

Throughout this book, we will be creating three powerful desktop applications, learning about the variety of widgets available in Tkinter. After we have these applications polished, we will then learn how to share them with other people by packaging them up for sharing. When you have finished this book, you will have in-depth knowledge of Tkinter, its widgets, GUI-design principles, and packaging Python projects.

Who this book is for

Do you have a great Python script, which runs via the command line that you wish could have a nice user interface? Or, do you know of a problem which could be resolved with a program containing a graphical interface? If so, this book is for you. All you need is some basic Python knowledge—things such as function declarations, if statements, for loops, and writing to files via the with statement. Everything else, including the use of Python's class system, will be covered in as much detail as you should need to complete each chapter.

What this book covers

Chapter 1, Meet Tkinter, introduces us to the Tkinter library itself. We will look at ensuring that it is installed, how to use its widget system, and how to display widgets inside a window. Once the basics are down, we will have a play with some of the easier widgets, including using a Label widget to display text and a Button widget to provide interactivity.

Chapter 2, Back to the Command Line – Basic Blackjack, has us return to our roots as Python programmers and create a command-line only version of blackjack. This will get us up to speed with the common situation where we a command-line application, which we want to convert to a nicer interface. We will also discuss Python's class system and how to structure an application for conversion to a graphical interface.

 Chapter 3, Jack is Back in Style – the Blackjack GUI, starts off the process of converting command-line blackjack to a graphical application. We will learn about the powerful Canvas widget and how we can use it to draw both shapes and images on the user's screen. This chapter will end with us having a working game of blackjack with a graphical interface.

Chapter 4, The Finishing Touches – Sound and Animation, teaches how to use the Canvas widget to create animations, which we will use to spruce up our game of blackjack. Of course, animations wouldn't be complete without accompanying sounds, so we will also have a look at how to play sound effects.

Chapter 5, Creating a Highly Customizable Python Editor, begins our second application, a text editor. We explore Tkinter's Text widget and its capabilities for handling events. We will also look at the themed widgets, which come with Tkinter in its ttk module, giving our applications a much more professional look.

Chapter 6, Color Me Impressed! – Adding Syntax Highlighting, covers how we can use Tkinter's tag system to affect different parts of certain widgets. We will be taking advantage of this system to add syntax highlighting for the Python language to our text editor. We will also explore the indexing system, which Tkinter uses to locate items inside some of its widgets.

Chapter 7, Not Just for Restaurants – All about Menus, teaches how we can add different types of menu to our applications—from the top menu bar that most applications have, to right-click context menus.

Chapter 8, Talk Python to Me – a Chat Application, begins our third and final application, an online instant messenger. We will learn how to plan the layout for a more complicated application, then piece together all of the different components needed for a chat program. We will also learn how to combine images and text by implementing smileys into the chat.

Chapter 9, Connecting – Getting our Chat Application Online, explains how we can use web technologies, including flask, requests, and sqlite3, to get desktop GUI applications communicating with the internet.

Chapter 10, Making Friends – Finishing our Chat Application, covers adding a friend's system and blocking system to improve socializing within our application. We will also learn how to manipulate images in Python using PIL by introducing user avatars. We will also learn why threads are great for computationally expensive or repeated tasks inside a GUI application.

Chapter 11, Wrapping Up – Packaging our Applications to Share, finishes off the book by briefly covering some widgets, which we did not get the opportunity to use in our three example application. Afterward, we will look at packaging a Python and Tkinter application up for distribution among users of the three biggest desktop operating systems, Windows, Linux, and macOS.

To get the most out of this book

This book assumes that you have:

  • A basic understanding of the Python language and its syntax, including functions, if statements, while and for loops, and file handling
  • A computer (desktop or laptop) running Windows, Linux, or macOS, which has Python version 3.6 (or higher) installed
  • Pip and Virtualenv installed along with Python
  • An internet connection to download any external dependencies, which will be needed for our projects

Download the example code files

You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packtpub.com.
  2. Select the SUPPORT tab.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Tkinter-GUI-Programming-by-Example. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: http://www.packtpub.com/sites/default/files/downloads/TkinterGUIProgrammingbyExample_ColorImages.pdf.

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "To specify the position within the grid, the row, and column keywords are used."

A block of code is set as follows:

self.label_text = tk.StringVar()
self.label_text.set("Choose One")

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

def say_hello(self):
    message = "Hello there " + self.name_entry.get()
msgbox.showinfo("Hello", message)

Any command-line input or output is written as follows:

>>> import tkinter
>>> tkinter.TkVersion

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "The last thing to do is to create the method that will be responsible for placing it into our Tools menu."

Note

Warnings or important notes appear like this.

Note

Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packtpub.com.