Discover millions of ebooks, audiobooks, and so much more with a free trial

From $11.99/month after trial. Cancel anytime.

Building Modern GUIs with tkinter and Python: Building user-friendly GUI applications with ease (English Edition)
Building Modern GUIs with tkinter and Python: Building user-friendly GUI applications with ease (English Edition)
Building Modern GUIs with tkinter and Python: Building user-friendly GUI applications with ease (English Edition)
Ebook611 pages2 hours

Building Modern GUIs with tkinter and Python: Building user-friendly GUI applications with ease (English Edition)

Rating: 0 out of 5 stars

()

Read preview

About this ebook

Are you looking to create stunning graphical user interfaces (GUIs) using Python? Look no further. This comprehensive guide will take you on a journey through the powerful capabilities of tkinter, Python's standard GUI library.

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.
LanguageEnglish
Release dateJun 28, 2023
ISBN9789355518576
Building Modern GUIs with tkinter and Python: Building user-friendly GUI applications with ease (English Edition)

Read more from Saurabh Chandrakar

Related authors

Related to Building Modern GUIs with tkinter and Python

Related ebooks

Programming For You

View More

Related articles

Reviews for Building Modern GUIs with tkinter and Python

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    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

    Enjoying the preview?
    Page 1 of 1