Book Image

Tkinter GUI Application Development Blueprints

By : Bhaskar Chaudhary
Book Image

Tkinter GUI Application Development Blueprints

By: Bhaskar Chaudhary

Overview of this book

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.
Table of Contents (15 chapters)
Tkinter GUI Application Development Blueprints
About the Author
About the Reviewers

The root window – your drawing board

GUI programming is an art, and like all art, you need a drawing board to capture your ideas. The drawing board that you will use is called the root window. Our first goal is to get the root window ready.

The following screenshot depicts the root window that we are going to create:

Drawing the root window is easy. You just need the following three lines of code:

from tkinter import *
root = Tk()

Save this with the .py file extension or check out the code. Open it in the IDLE window and run the program from the Run menu (F5 in IDLE). Running this program should generate a blank root window, as shown in the preceding screenshot. This window is equipped with the functional minimize, maximize, and close buttons, and a blank frame.


Downloading the example code

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

The following is a description of the preceding code:

  • The first line imported all (*) the classes, attributes, and methods of Tkinter into the current workspace.

  • The second line created an instance of the tkinter.Tk class. This created what is called the "root" window, which is shown in the preceding screenshot. According to the conventions, the root window in Tkinter is usually called "root", but you are free to call it by any other name.

  • The third line executed the mainloop (that is, the event loop) method of the root object. The mainloop method is what keeps the root window visible. If you remove the third line, the window created in line 2 will disappear immediately as soon as the script stops running. This will happen so fast that you will not even see the window appearing on your screen. Keeping the mainloop method running also lets you keep the program running until you press the close button, which exits the main loop.

  • Tkinter also exposed the mainloop method as tkinter.mainloop(). So, you can even call mainloop() directly instead of calling root.mainloop().

Congratulations! You have completed your first objective, which was to draw the root window. You have now prepared your drawing board (root window). Now, get ready to paint it with your imagination!


Commit the three lines of code (shown in code to memory. These three lines generate your root window, which will accommodate all the other graphical components. These lines form the skeleton of any GUI application that you will develop in Tkinter. The entire code that will make your GUI application functional will go between line 2 (new object creation) and line 3 (mainloop) of this code.