Building Modern GUIs with tkinter and Python: Building user-friendly GUI applications with ease (English Edition)
()
About this ebook
This comprehensive guide explores the power of Python's tkinter library. This book covers various classes of GUI widgets, including buttons, input fields, displays, containers, and item widgets. It teaches you how to create interactive and visually appealing user interfaces, handle file selection, gather widget information, and trace changes. Additionally, it includes a hands-on project on creating a user login system using tkinter and sqlite3 database. Whether you're a beginner or an experienced developer, this book will empower you to build professional and intuitive GUI applications effortlessly.
By the end of the book, you will have gained knowledge and skills in creating modern user interfaces using the tkinter Python library.
Read more from Saurabh Chandrakar
Python GUI with PyQt: Learn to build modern and stunning GUIs in Python with PyQt5 and Qt Designer (English Edition) Rating: 0 out of 5 stars0 ratingsPython for Everyone: Learn and polish your coding skills in Python (English Edition) Rating: 0 out of 5 stars0 ratingsProgramming Techniques using Python: Have Fun and Play with Basic and Advanced Core Python Rating: 0 out of 5 stars0 ratings
Related to Building Modern GUIs with tkinter and Python
Related ebooks
GUI Magic: Mastering Real Projects in Python Rating: 0 out of 5 stars0 ratingsMASTERING PYCHARM: Use PyCharm with fluid efficiency to write idiomatic python code Rating: 5 out of 5 stars5/5NumPy Recipes Rating: 0 out of 5 stars0 ratingsRacket Unleashed: Building Powerful Programs with Functional and Language-Oriented Programming Rating: 0 out of 5 stars0 ratingsInteractive Applications Using Matplotlib Rating: 0 out of 5 stars0 ratingsPython Unlocked Rating: 0 out of 5 stars0 ratingsMastering Python Programming: From Basics to Expert Proficiency Rating: 0 out of 5 stars0 ratingsFunctional Programming in Python: From Basics to Expert Proficiency Rating: 0 out of 5 stars0 ratingsModular Programming with Python Rating: 0 out of 5 stars0 ratingsFeedforward Neural Networks: Fundamentals and Applications for The Architecture of Thinking Machines and Neural Webs Rating: 0 out of 5 stars0 ratingsMastering Three.js: A Journey Through 3D Web Development Rating: 0 out of 5 stars0 ratingsPython for Machine Learning: From Fundamentals to Real-World Applications Rating: 0 out of 5 stars0 ratingsNeural Networks with Python Rating: 0 out of 5 stars0 ratingsDjango 1.1 Testing and Debugging Rating: 4 out of 5 stars4/5The JavaScript Journey: From Basics to Full-Stack Mastery Rating: 0 out of 5 stars0 ratingsOpenCV Android Programming By Example: Leverage OpenCV to develop vision-aware and intelligent Android applications. Rating: 0 out of 5 stars0 ratingsData Structure in Python: From Basics to Expert Proficiency Rating: 0 out of 5 stars0 ratingsUltimate Neural Network Programming with Python Rating: 0 out of 5 stars0 ratingsComputer Vision for the Web: Unleash the power of the Computer Vision algorithms in JavaScript to develop vision-enabled web content Rating: 0 out of 5 stars0 ratingsOpenFlow Cookbook Rating: 5 out of 5 stars5/5Python AI Programming Rating: 0 out of 5 stars0 ratingsMastering OpenCV with Python Rating: 0 out of 5 stars0 ratingsGroup Method of Data Handling: Fundamentals and Applications for Predictive Modeling and Data Analysis Rating: 0 out of 5 stars0 ratingsDynamic Programming in Java: From Basics to Expert Proficiency Rating: 0 out of 5 stars0 ratingsLearning jqPlot Rating: 0 out of 5 stars0 ratingsNoSQL Essentials: Navigating the World of Non-Relational Databases Rating: 0 out of 5 stars0 ratingsLogic Programming: Fundamentals and Applications Rating: 0 out of 5 stars0 ratingsBio Inspired Computing: Fundamentals and Applications for Biological Inspiration in the Digital World Rating: 0 out of 5 stars0 ratings
Programming For You
Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Excel 101: A Beginner's & Intermediate's Guide for Mastering the Quintessence of Microsoft Excel (2010-2019 & 365) in no time! Rating: 0 out of 5 stars0 ratingsJavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Coding All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsHTML in 30 Pages Rating: 5 out of 5 stars5/5Hacking Electronics: Learning Electronics with Arduino and Raspberry Pi, Second Edition Rating: 0 out of 5 stars0 ratingsTiny Python Projects: Learn coding and testing with puzzles and games Rating: 4 out of 5 stars4/5Microsoft Azure For Dummies Rating: 0 out of 5 stars0 ratingsProblem Solving in C and Python: Programming Exercises and Solutions, Part 1 Rating: 5 out of 5 stars5/5Learn SQL in 24 Hours Rating: 5 out of 5 stars5/5Coding with JavaScript For Dummies Rating: 0 out of 5 stars0 ratingsMastering Windows 365: Deploy and Manage Cloud PCs and Windows 365 Link devices, Copilot with Intune, and Intune Suite Rating: 0 out of 5 stars0 ratings
Reviews for Building Modern GUIs with tkinter and Python
0 ratings0 reviews
Book preview
Building Modern GUIs with tkinter and Python - Saurabh Chandrakar
C
HAPTER
1
tkinter Introduction
Introduction
We have learned in our previous book Python for Everyone, about the concepts in Python which are procedure oriented or object oriented. However, these concepts will be used as indispensable salt for our next learning, which is related to Graphical User Interface (GUI). We are surrounded by GUI apps in day-to-day life. Whenever we are using our mobile phone/Desktop applications and accessing any app or software, the first thing which we look forward to is how to access these apps or software. Any app on mobile phone or a computer system consists of hardware and is controlled by an operating system. The high-level languages will be sitting on top of the operating system and Python is no exception. So, in this chapter, we will learn about tkinter library in Python.
Structure
In this chapter, we will discuss the following topics:
Introduction to tkinter
Basic Python GUI Program
Some standard attributes of Python tkinter GUI
Colors
Fonts
Anchors
Relief Styles
Bitmaps
Cursors
Python tkinter Geometry Manager
Objectives
By the end of this chapter, the reader will learn about creating basic GUI Python program using the tkinter library. In addition to that, we will also explore some standard attributes of Python tkinter GUI such as dimensions, colors or fonts with various options with examples. It is important to know how to position the text with a list of constants, relative to the reference point using anchor attribute. Moreover, we shall see how with the usage of relief attribute 3-D, simulated effects around the outside of the widget can be provided. We will also learn about bitmap and cursor attribute with examples. Finally, at the end of this chapter, we will learn accessing tkinter widgets using inbuilt layout geometry managers viz pack, grid and place.
Introduction to tkinter
Whenever we write any program in Python to control the hardware, Python will show the output with the help of operating system. However, if we desire to make an executable with the help of GUI, then just having the need of hardware and operating system is insufficient. Python requires some services which come from a number of resources and one such resource which is of interest to many Python programmers is Tcl/Tk. Tcl stands for Tool Command Language and it is a scripting language with its own interpreter. On the other hand, Tk is a toolkit for building GUIs. An important point to note is that Tcl/Tk is not Python and we cannot control and access the services of Tcl/Tk using Python. So, another package is introduced and is referred to as tkinter, and it is an intermediator between Python and Tcl/Tk. tkinter will allow us to use the services of Tcl/Tk using the syntax of Python. As Python code developers, we will not be directly concerned with the Tcl/Tk. Binding of Python to the Tk GUI toolkit will be done by tkinter. tkinter will make everything appear as an object. We can create GUI, knowing that we can regard the window as an object, a label place on window as an object and so on. Applications can be built from a view point of an object-oriented programming paradigm. All we need to make sure is that we write our code in such a way that it allows us access tkinter, as shown in the following Figure 1.1:
Figure 1.1: tkinter access hierarchy
So, GUI applications can be easily and quickly created when Python is combined with tkinter. Although Python offers multiple options for developing GUI such as PyQT, Kivy, Jython, WxPython, and pyGUI; tkinter is the most commonly used. In this book, we will focus only on the tkinter usage of GUI creation. Just like we import any other module, tkinter can be imported in the same way in Python code:
import tkinter
This will import the tkinter module.
The module name in Python 3.x is tkinter, whereas in Python 2.x, it is Tkinter.
More often, we can use:
from tkinter import *
Here, the ‘*’ symbol means everything, as Python now can build Graphical User Interfaces by treating all of the widgets like Buttons, Labels, Menus and so on, as if they were objects. It is like importing tkinter submodule.
However, there are some important methods which the user needs to know while creating Python GUI application, and they are as follows:
Tk(screenName=None, baseName=None, className=’Tk’, useTk=1)
tkinter offers this method in order to create a main window. For any application, a main window code can be created by using:
import tkinter
myroot = tkinter.Tk()
Here,Tk class is instantiated without any arguments.myroot is the main window object name. This method will allow blank parent window creation with close, maximize and minimize buttons on the top.
mainloop()
tkinter offers this method when our application is ready to run. This method is an infinite loop used to run the application. It will wait for an event to occur and as long as the window is not closed, the event is processed.
Basic Python GUI program
Let us see a basic Python program which will create a window:
Output:
The output can be seen in Figure 1.2:
Figure 1.2: Output of Chap1_Example1.py
In the preceding code, we have imported tkinter submodule and created a parent widget which usually will be the main window of an application. A blank parent window is created with close, maximize and minimize buttons on the top. An infinite loop will be called to run the application, as long as the window is not closed.
The moment the window is closed, the statements after myroot.mainloop() will be executed, as shown in the following Figure 1.3:
Figure 1.3: Output on running the code
The moment the window is closed by clicking on the ‘X’ mark, we will get the output as shown in Figure 1.4:
Figure 1.4: Execution of print statement after clicking ‘X’ mark
In the preceding example, we can maximize the window in both horizontal and vertical directions by using both height and width attributes. However, we can restrict the expansion of window in any direction.
Suppose we want to maximize the width only. Then, the code is as follows:
Output:
The output can be seen in Figure 1.5:
Figure 1.5: Output of Chap1_Example2.py
Suppose, we want to maximize the height only. Then the code is as follows:
Output:
The output can be seen in Figure 1.6:
Figure 1.6: Output of Chap1_Example3.py
Now, there is a need to maximize neither height nor width. Then the following code will be used:
Output:
The output can be seen in Figure 1.7:
Figure 1.7: Output of Chap1_Example4.py
An important point to observe is that when we are maximizing the window in either of the directions, then the maximize button was enabled. Whereas in this case, the maximize button is disabled.
Some standard attributes of Python tkinter GUI
Now, we shall see how some of the standard attributes such as sizes, colors or fonts are specified. Just observe the standard attributes as mentioned. We shall see their usage when we will be dealing with widgets.
Dimensions
Whenever we set a dimension to an integer, it is assumed to be in pixels. A length is expressed as an integer number of pixels by tkinter. The list of common options are discussed as follows.
borderwidth
This option will give a 3-D look to the widget. It can also be represented as bd:
Output:
The output can be seen in Figure 1.8:
Figure 1.8: Output of Chap1_Example5.py
highlightthickness
This option represents the width of the highlighted rectangle when the widget has focus. Refer to the following code:
Output:
The output can be seen in Figure 1.9:
Figure 1.9: Output of Chap1_Example6.py
padX, padY
This option will provide extra space that the widget requests from its layout manager, beyond the minimum, for the display of widget contents in x and y directions. We can see in the previous example, that we have used padding in x and y direction for better look and display.
wraplength
This option will provide maximum length of line for widgets which will be performing word wrapping. Refer to the following