Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Tkinter GUI Application Development Blueprints, Second Edition

You're reading from   Tkinter GUI Application Development Blueprints, Second Edition Build nine projects by working with widgets, geometry management, event handling, and more

Arrow left icon
Product type Paperback
Published in Mar 2018
Publisher Packt
ISBN-13 9781788837460
Length 422 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Bhaskar Chaudhary Bhaskar Chaudhary
Author Profile Icon Bhaskar Chaudhary
Bhaskar Chaudhary
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Meet Tkinter FREE CHAPTER 2. Making a Text Editor 3. Programmable Drum Machine 4. Game of Chess 5. Building an Audio Player 6. Paint Application 7. Piano Tutor 8. Fun with Canvas 9. Multiple Fun Projects 10. Miscellaneous Tips 11. Other Books You May Enjoy

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:

import tkinter as tk
root = tk.Tk() #line 2
root.mainloop()

Save this with the .py file extension or check out the code present in the 1.01.py file. Open it in the IDLE window or run it from within your activated virtual environment using the following command:

$ python 1.01.py

Running this program should generate a blank root window, as shown in the preceding screenshot. This window is equipped with 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 http://www.packtpub.com. Apart from going to Packt's official website, you can also find the code files for this book at https://github.com/PacktPublishing/Tkinter-GUI-Application-Development-Blueprints-Second-Edition. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files emailed directly to you.

The following is a description of the preceding code:

  • The first line imported the tkinter module into the namespace with tk as its alias. Now we can access all definitions of the classes, attributes, and methods of Tkinter by appending the alias tk to the name as in tk.Tk().
  • 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 mainloop.
  • 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 1.01.py) 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.
You have been reading a chapter from
Tkinter GUI Application Development Blueprints, Second Edition - Second Edition
Published in: Mar 2018
Publisher: Packt
ISBN-13: 9781788837460
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image