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

Tkinter's indexing system


Indexing is handled in a somewhat coordinate-based way. An index is represented by two numbers separated by a single full stop. For example: 4.5.

The first number (before the .) in this index can be thought of as the line number. This begins at 1.

The second number (after the .) is how many characters into the line we are. This begins at 0.

The first character within a Text widget will therefore be located at 1.0. This means line 1, 0 characters in.

To ensure we fully understand this concept, let's create a demo application which will show us where the cursor is located at all times.

Getting the cursor's position

Open up a new Python file and enter the following code:

import tkinter as tk

win = tk.Tk()
current_index = tk.StringVar()
text = tk.Text(win, bg="white", fg="black")
lab = tk.Label(win, textvar=current_index)

Begin with the normal importing and creation of a main window.

The things we will need for this application are a StringVar to hold the current cursor location...