Create and Show Root Window of tkinter GUI Program

By | March 22, 2020

Topic: Create and Show tkinter Root Window

What is a Root Window in tkinter

A Python 3 Graphical User Interface Program always contains a root window . The root window is the main window of the Python GUI program. This main window will have the other GUI elements which are called widgets. For example, we can use widgets like button, lable, entry,  radio button and check button etc.

Create and Show tkinter Root Window

Create and Show tkinter Root Window

Creating a Root Window in tkinter

The tkinter GUI module provides a class Tk. We use the contructor of Tk class to craete an instance which is the root window of the program.

The Python 3 Code To Create a Root Window

window = tk.Tk()

The Complete Code for tkinter Root Window

# Write a Python GUI program
# using tkinter module
# to diplay a Root window

import tkinter as tk

def main():
    window= tk.Tk()
    
    window.mainloop()

main()

Explanation of Code in the Image

  1. Comments
  2. Comments
  3. Comments
  4. Blank
  5. Imports tkinter module, use tk as an alias. Because tk is short and easy to use in code.
  6. Blank
  7. Header of main() function.
  8.  Indented body of main() function . Create main window or Root window of the application.
  9. Blank
  10. When this line of code will execute, it will start the mainloop that is event loop. So that the output root window is shown to the user. It will wait for any actions called events performed by the user and the code attached, if any, will respond to those events. For example, if we click on close button, the window will be closed.
  11. Call to main() function. Now the execution of the program will start and the root window will be displayed on the screen.

Loading

3 thoughts on “Create and Show Root Window of tkinter GUI Program

  1. Pingback: Find Factorial by Recursive Function Python GUI Program | EasyCodeBook.com

  2. Pingback: Python GUI Multiplication Table | EasyCodeBook.com

  3. Pingback: Python Digital Clock Program using tkinter GUI | EasyCodeBook.com

Leave a Reply

Your email address will not be published. Required fields are marked *