In this tutorial, we will learn about Tkinter Windows in Python which is the main Window of the GUI application inside which every other component runs. We have covered the basic Tkinter GUI application Components in which we have explained how Tkinter Windows, Widgets, and Frames are building blocks of a Tkinter App.
The Tkinter window is the foundational element of the Tkinter GUI. Tkinter window is a container in which all other GUI elements(widgets) live.
Here is the syntax for creating a basic Tkinter Window:
win = Tk()
Yes, we use the Tk() function to create our Tkinter app window in which all the other components are added.
Tkinter Windows Example:
Here is a simple example,
from tkinter import * win = Tk() # run the app window win.mainloop()
In the above example, the
mainloop() function is used to run the GUI application.
Tkinter Customized Window
Let us now cover a basic example where we will create a Basic GUI Application using properties like title and geometry.
Here we have the code to demonstrate the steps used in the creation of a customized Tkinter Window:
from tkinter import * window = Tk() # You can add your widgets here window.title('Hello StudyTonight') window.geometry("300x200+10+20") window.mainloop()
Here is what we have done in the code:
The first step is to import the Tkinter module in the code.
After importing, the second step is to set up the application object by calling the
Tk()function. This will create a top-level window (root) having a frame with a title bar and control box with the minimize and close buttons, and a client area to hold other widgets.
After that, you can add the widgets you might want to add in your code like buttons, textbox, scrollbar, labels, and many more.
window.title()function is used to provide the title to the user interface as you can see in the output.
In the line
geometry()method defines the width, height, and coordinates of the top left corner of the frame as follows (all values are generally in pixels) in the same way. Here is the syntax:
After that, the application object enters an event listening loop by calling the
mainloop()method. In this way, the application is now constantly waiting for any event generated on the elements in it. There could be an event like text entered in a text field, a selection made from the dropdown or radio button, single/double click actions of the mouse, etc.
The application's functionality involves executing appropriate callback functions in response to a particular type of event.
The event loop will terminate as soon as there is a click made on the close button on the title bar.
In this tutorial, we learned how to create a Tkinter window to create a GUI application. The Tkinter window contains all the widgets that make the application.