A graphical user interface (GUI) is a desktop interface that allows you to communicate with computers. They carry out various activities on desktop computers, laptops, and other mobile devices. Text-Editors and other graphical user interface applications build, read, download, and erase various types of files. You can also play games such as Sudoku, Chess, and Solitaire through these apps. Google Chrome, Firefox, and Microsoft Edge are examples of graphical user interface (GUI) Internet browsers.
Python has a variety of libraries, but these four stand out, especially in terms of GUI. Â
- TkinterÂ
- KivyÂ
- Python QTÂ
- wxPython
Tkinter is the first option for a lot of learners and developers because it is quick and convenient to use. Tkinter is a Python library that can be used to construct basic graphical user interface (GUI) applications. In Python, it is the most widely used module for GUI applications.
Next up, let’s get started with Tkinter.
Building Your First Python GUI Application With Tkinter
To start building your application Tkinter, the first step is to install the Tkinter module in the python shell.
The next step is to open a window in Tkinter. The below code will open a window instance on your system.
The above code will open a window like the one given below. Please note that the appearance of the window will depend on your operating system.
-
Adding a Widget
Before adding a widget, let’s rename the window title to GUI. You can do this by adding the window.title and give the name that you want the title to be.
The next step is adding a label widget to the window. A label is something that we want the output screen to show. To add the label to the window, we have used the label widget’s pack method.
Working With Widgets
In python, widgets work similarly to HTML elements. Various types of Tkinter correspond to different types of elements.
Some widgets available in Tkinter are given below:
Widget |
Description |
Canvas |
Canvas is used in the GUI to draw shapes. |
Button |
The button widget is used to display buttons in the Tkinter. |
Checkbutton |
Checkbutton is a class that is used to build checkboxes in the program. |
Entry |
The entry widget is used in the GUI to construct input fields. |
Frame |
In Tkinter, frames are used as containers. |
Label |
A label is used to generate single-line widgets such as text, photographs, and many more. |
Menu |
The menu is included in the GUI to build menus. |
LabelFrame |
It is a container widget. Its main function is to serve as a spacer or container for complex window formats. |
Menubutton |
A menubutton is a component of a drop-down menu that remains visible on the screen at all times. Each menubutton is linked to a Menu widget, which displays the menubutton's options when the user clicks on it. |
Scrollbar |
This widget includes a slide controller for implementing vertically scrolled widgets like Listbox, Text, and Canvas. |
Scale |
When we try to pick a single value from a set of values, we use the Scale widget. It has a sliding bar from which we can pick values by sliding from left to right or top to bottom. |
Notebook |
This widget is used to select pages of content by clicking on tabs. |
Combobox |
A Combobox widget allows you to choose one attribute from a set of options. It also helps you to enter a custom value. |
Spinbox |
The Spinbox widget is a variation of the basic Tkinter Entry widget that allows you to choose from a defined set of values. |
Separator |
A separator widget adds a thin horizontal or vertical rule between widget types. |
Progressbar |
A Progressbar widget helps you to inform the user about the status of a long-running mission. |
Sizegrip |
It gives you the option to resize the enter application window. |
Treeview |
TreeView enables one to build a tree-like structure and insert objects together with their attributes. |
-
Displaying Text and Images With Label Widgets
Label widgets are used to add text or images to the window. As we saw in the last example, we can add a label widget by creating the Label class. To add a text to the widget, we pass a string to the text parameter.
By default, the colour of the text and background is set as black and white respectively, but you can change the same by adding more parameters such as the background to give the color of your choice.
Now, as you can see here, the text appears in the default color i.e black and since we changed the black color, it appears in red.
-
Displaying Clickable Buttons With Button Widgets
The button widgets are used to create clickable buttons. It is very similar to the label widget. In this section, we will only look at how to style the buttons and not the functioning of the buttons.Â
Output-
-
Getting User Input With Entry Widgets
The entry field is used to take input from the user in GUI
The entry class here will create a text box in the window, where the user can input any text.
Output-
As you can see here, we have entered the text as Python in the text field created by the entry class.
-
Getting Multiline User Input With Text Widgets
Text widgets work similarly as entry widgets. But the major difference is that in a text widget, we can enter the input in multiple lines unlike in the entry widget.
We create a Text and .pack() widget to get the text field displayed in the window.
Output-
When we use the Text class, the whole window acts as a text field, and we can write the input in whatever form we like, in multiple lines or a paragraph.
-
Assigning Widgets to Frames With Frame Widgets
A frame is a rectangular area that can contain other widgets in Tkinter. It is used to organize complex layouts and does not have any special styling of its own.
In the example below, we create two frames, and further, in each frame we created a label widget, containing some text.Â
Output-
In the code above, we called frame_a first, therefore its text appears first, but if we reverse the order in which they are called, the output will also be reversed.
-
Understanding Widget Naming Conventions
Widget follows a structure close to that of package naming. The root window in Tk is called with a duration (.) and an entity in the window, such as a button, is named .myButton1. A lowercase letter, digit, or punctuation mark (except a period) should be used to begin the variable name. Following the first character, the remaining characters may be uppercase or lowercase letters, numbers, or punctuation marks (except periods). It is best to begin the label with a lowercase letter.
Controlling Layout With Geometry Managers
All Tkinter widgets have certain geometry dimensions. These geometry measurements allow you to arrange the widgets and parent frames or parent widget space.
Tkinter has three major geometry manager classes:
pack(): It organizes the widgets in a block-like fashion, taking up the whole available width. It's a standard way of displaying widgets in the browser.
grid(): This function arranges the widgets in a table-like arrangement.Â
place(): Its function is to place the widgets as specified by the user in the parent widget.
Now, let us look at each of these classes in detail.
-
The .pack() Geometry Manager
The pack() approach primarily implements a packaging algorithm to arrange widgets in a Frame or window in a certain order.
This approach is defined as used to arrange widgets in a block.
This algorithm will first compute a rectangular area known as a Parcel that is tall (or wide) enough to hold the widget, and then it will fill the remaining width (or height) in the window with blank space.
Unless a separate position is defined, it will focus on the widget.
Output-
As you can see here, the different frames have been arranged in the decreasing order of their height and width, and also the order of their method calling.
-
The .place() Geometry Manager
The place() Geometry Manager organizes widgets so that they can be placed in a given location as specified by the programmer.
This approach essentially organizes the widget based on the x and y coordinates. Both the x and y coordinates are expressed in pixels.
As a result, the center (where x and y are all 0) is the top-left corner of the Frame or window.
Thus, the y argument specifies the number of pixels to position the widget from the top of the display, and the x argument specifies the number of pixels to place the widget from the left of the window.
Output-
-
The .grid() Geometry Manager
The grid() geometry manager arranges widgets in a tabular fashion. In the method call, we can define the rows and columns as choices. A widget's column-span (width) or row-span (height) may also be defined.
This is a more streamlined method of adding widgets to a Python program.
Output-
Find Our Python Training in Top Cities |
Making Your Applications Interactive
-
Using Events and Event Handlers
To begin the event loop, you must call the window.mainloop() when you build a Tkinter program. During the incident loop, the application determines whether or not an occurrence has happened. If this is the case, any code can be executed in response.
Tkinter has an event loop, so you don't have to write any code to search for events yourself. You must, therefore, write the code that will be executed in response to an occurrence. For the events that you use in your program, you write functions called event handlers in Tkinter.
-
Using .bind
.bind() is used to invoke an event handler if an event happens on a widget. Since it is called any time the event happens, the event controller is considered to be tied to the event.Â
-
Using command
Every Button widget has a command attribute that can be used to add a feature to it. The operation is performed once the button is pushed.
Don't miss out on the opportunity to become a Certified Professional with Simplilearn's Post Graduate Program in Full Stack Web Development. Enroll Today!
Conclusion
That was all about the Tkinter module in python, and now by learning these basics you are able to create your first application using Tkinter.
You should also step up your software development game, and gain proficiency in different programming languages, by enrolling in Simplilearn's Post Graduate Program in Full Stack Web Development.
And if you have any doubts, feel free to drop them in the comments section and our experts will help you out.