Tkinter Windows, Widgets and Frames
In this tutorial, we will cover the basics of the Tkinter module, to explain what is a Tkinter Window, Widgets, and Frames which are the building blocks of GUI application in Tkinter.
Introduction to Tkinter Windows and Widgets
Let's dive in a little deeper, to understand some basics about the Tkinter module and how it works.
The top-level window object in GUI Programming contains all of the little window objects that will be part of your complete GUI.
The little window objects can be text labels, buttons, list boxes, etc., and these individual little GUI components are known as Widgets.
So, having a top-level window object will act as a container where you will put all your widgets. In Python, you'd typically do so like this using the following code:
win = tkinter.Tk()
The object that is returned by making a call to
tkinter.Tk() is usually referred to as the Root Window.
Top-level windows are mainly stand-alone as part of your application, also you can have more than one top-level window for your GUI, but only one of them should be your root window.
First of all, you need to design all your widgets completely, and then add the real functionality.
The widgets can either be stand-alone or can be containers. If one widget contains other widgets, it is considered the parent of those widgets.
Similarly, if a widget is contained within another widget, it's known as a child of the parent, the parent is the next immediate enclosing container widget.
The widgets also have some associated behaviors, such as when a button is pressed, or text is filled into a text field, so we have events attached to these actions.
The behavior of widgets generates events, and the GUI's response to events is known as Callbacks - because they 'call' a function just to handle the event that occurred.
Tkinter Event-Driven Processing
In Tkinter, we have windows and widgets coming together to form a GUI application. But the GUI application is just the frontend of the application. You would want to execute some code logic when the end-user uses those widgets.
Whenever an action is performed on any widget, an event is generated, which we can handle to perform any operation.
Events(behavior of widgets) can include pressing a button, movement of the mouse, hitting the return or Enter key, gaining or losing 'focus', etc.
The entire system of events that occurs from the beginning until the end of any GUI application is what drives it and hence it is also known as Event-Driven Processing.
Let us take a simple mouse movement example: Suppose that the pointer of the mouse is just sitting somewhere on top of your GUI application. If you will move the mouse to another part of your application, something has to cause the movement of the mouse to be replicated by the cursor on your screen(on top of your GUI application). These are 'cursor move' events that the system must process to portray your cursor moving across the window. At the time you will stop moving the mouse, no more events need to be processed, so everything just stays still on the screen again.
Here are some basic definitions through which you will be able to understand the concept of Windows, widgets, and frames in Tkinter.
The term "Window" has different meanings in the different contexts, But generally "Window" refers to a rectangular area somewhere on the user's display screen through which you can interact.
Then there comes the concept of Top Level Window in Tkinter.
Tkinter Top-Level Window
The Top-Level Window is a window that exists independently on the screen. You can decorate the top-level window with the standard frame and controls for the desktop manager. It can usually be moved around the desktop, and also you can resize it if you want to do so.
Then there comes the concept of Widgets. Let us try to understand it.
The term "Widgets" is a generic term that refers to the building blocks that make up an application in a graphical user interface.
Let us list out the core widgets with their categories:
Under this category, the widgets that lies are frame, labelframe, toplevel, and paned window.
Under the category of Buttons, there are buttons, radiobuttons, checkbuttons (checkbox), and menubuttons.
Under the category of text widgets, there are labels, messages, text.
Under this category, the widgets are scale, scrollbar, Listbox, slider, spinbox, entry (single-line), optionmenu, text (multiline), and canvas (vector and pixel graphics).
Now let us move on to Frames in Tkinter.
A frame is basically a rectangular area that can contain other widgets. In Tkinter, there is a Frame widget that is the basic unit of organization for complex layouts. It is a widget which has no special styling or GUI component of its own. It is just used to hold other Tkinter widgets in case of complex GUI layouts.
Note: It is important to note here that whenever any widget is created, there is also a parent-child relationship that is created. Just take an example, if you place a button inside a frame, the frame widget is called the parent of the Button widget.
Tkinter Basic Example
Let us take an example where we will create a Tkinter application with a simple Text widget:
from tkinter import *
win = Tk() # Create the root (base) window
w = Label(win, text="Hello, Welcome to Studytonight!") # Create a label with words
w.pack() # Put the label into the window
win.mainloop()# Start the event loop
The above code will create a window with the label widget and output will look like as shown above. We have created a Tkinter Window and then added a basic Label widget to it.
In this tutorial, we learned about the basic building blocks of GUI application using Tkinter which are Windows, Widgets, and Frames, which are used to develop different GUI applications. In the next tutorial, we will learn how to create a Tkinter Window which is the starting point for any application, because in a Tkinter Window we add all the widgets.