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 authors
Related to Building Modern GUIs with tkinter and Python
Related ebooks
Applied Deep Learning: Design and implement your own Neural Networks to solve real-world problems (English Edition) Rating: 0 out of 5 stars0 ratingsApplied Machine Learning Solutions with Python: SOLUTIONS FOR PYTHON, #1 Rating: 0 out of 5 stars0 ratingsSimulation-Driven Electronics Design: The easy way to design your own electronics projects (English Edition) Rating: 0 out of 5 stars0 ratingsGetting Started with UDOO Rating: 0 out of 5 stars0 ratingsPython Machine Learning Projects: Learn how to build Machine Learning projects from scratch (English Edition) Rating: 0 out of 5 stars0 ratingsFundamental Concepts of MATLAB Programming: From Learning the Basics to Solving a Problem with MATLAB Rating: 0 out of 5 stars0 ratingsLearn C Programming from Scratch: A step-by-step methodology with problem solving approach (English Edition) Rating: 0 out of 5 stars0 ratingsDeep Learning with C#, .Net and Kelp.Net: The Ultimate Kelp.Net Deep Learning Guide Rating: 0 out of 5 stars0 ratingsCookbook for Mobile Robotic Platform Control: With Internet of Things And Ti Launch Pad Rating: 0 out of 5 stars0 ratingsPython Object-Oriented Programming for Beginners Rating: 0 out of 5 stars0 ratingsIoT Data Analytics using Python: Learn how to use Python to collect, analyze, and visualize IoT data (English Edition) Rating: 0 out of 5 stars0 ratingsHands-On System Programming with C++: Build performant and concurrent Unix and Linux systems with C++17 Rating: 0 out of 5 stars0 ratingsMachine Learning in Production: Master the art of delivering robust Machine Learning solutions with MLOps (English Edition) Rating: 0 out of 5 stars0 ratingsDeep Neural Nets Deep Learning A Complete Guide Rating: 0 out of 5 stars0 ratingsMining for Knowledge: Exploring GPU Architectures In Cryptocurrency and AI: The Crypto Mining Mastery Series Rating: 0 out of 5 stars0 ratingsBackpropagation: Fundamentals and Applications for Preparing Data for Training in Deep Learning Rating: 0 out of 5 stars0 ratingsGenerative AI Application Integration Patterns: Integrate large language models into your applications Rating: 0 out of 5 stars0 ratingsImage Segmentation: Unlocking Insights through Pixel Precision Rating: 0 out of 5 stars0 ratingsDeep Neural Nets A Clear and Concise Reference Rating: 0 out of 5 stars0 ratingsHands-On Design Patterns with C++: Solve common C++ problems with modern design patterns and build robust applications Rating: 0 out of 5 stars0 ratingsApps and Services with .NET 8: Build practical projects with Blazor, .NET MAUI, gRPC, GraphQL, and other enterprise technologies Rating: 0 out of 5 stars0 ratingsHow to Design Optimization Algorithms by Applying Natural Behavioral Patterns Rating: 0 out of 5 stars0 ratingsPython Data Cleaning Cookbook: Prepare your data for analysis with pandas, NumPy, Matplotlib, scikit-learn, and OpenAI Rating: 0 out of 5 stars0 ratingsDigital Image Processing: Fundamentals and Applications Rating: 0 out of 5 stars0 ratingsComputer Architecture Rating: 5 out of 5 stars5/5Neural Networks with Python Rating: 0 out of 5 stars0 ratings
Programming For You
Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/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 ratingsLinux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Spies, Lies, and Algorithms: The History and Future of American Intelligence Rating: 4 out of 5 stars4/5Mastering Excel: Starter Set Rating: 2 out of 5 stars2/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 5 out of 5 stars5/5HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Coding with JavaScript For Dummies Rating: 0 out of 5 stars0 ratingsBeginning Programming with C++ For Dummies Rating: 4 out of 5 stars4/5HTML in 30 Pages Rating: 5 out of 5 stars5/5C Programming For Beginners: The Simple Guide to Learning C Programming Language Fast! Rating: 5 out of 5 stars5/5Python QuickStart Guide: The Simplified Beginner's Guide to Python Programming Using Hands-On Projects and Real-World Applications Rating: 0 out of 5 stars0 ratingsPoirot's Early Cases Rating: 5 out of 5 stars5/5
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