When do I need to call the main loop in a Tkinter application?


Python Tkinter is a standard library that is used to develop GUI-based featured and functional applications. Whenever we execute our application, it displays a general window that contains some widgets and a title bar in it. The mainloop() method is responsible for executing the script and displaying the output window. However, mainloop() implies that it doesn't terminate automatically until the user remains in the window. Whenever the user terminates the program, it gets closed automatically. The mainloop() method gets called whenever the program starts executing.

Example

# Import the required libraries
from tkinter import *

# Create an instance of tkinter frame
win = Tk()
win.geometry("700x350")

# Add a Label widget
Label(win, text="This is a New Line Text", font= ('Times New Roman', 18,'bold')).pack(pady= 40)
win.mainloop()

Output

Executing the above code will display a new window with some text message in it.

Updated on: 07-Jun-2021

688 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements