tkinter
  1. tkinter-toplevel

Toplevel - Tkinter Container Widgets

Tkinter is a Python GUI toolkit that provides widgets to create graphical user interfaces. One of the widgets that Tkinter provides is Toplevel. In this tutorial, we will explore the Toplevel widget in detail.

Syntax

The syntax for creating a Toplevel widget is as follows:

toplevel = Toplevel(master, **options)

Here:

  • master is the parent widget.
  • options are the configuration options for the Toplevel widget.

Example

Consider the following example which creates a Toplevel widget:

from tkinter import *

root = Tk()

toplevel = Toplevel(root)
toplevel.title("Toplevel Window")
toplevel.geometry("300x200")

label = Label(toplevel, text="This is a Toplevel Window")
label.pack(pady=50)

root.mainloop()

Here, we create a Tkinter window and a Toplevel widget whose parent is the root window. We set the title and dimensions of the Toplevel window. Finally, we create a Label widget in the Toplevel window.

Output

When you run the above example, you will see a Tkinter window with a button labelled "Click to open Toplevel". When you click the button, a new window labelled "Toplevel Window" will appear with a label "This is a Toplevel Window".

Explanation

The Toplevel widget is used to create additional windows that can be used as dialogs, message boxes, or separate windows. It can be created with a parent window which must be specified in the constructor. When you create a Toplevel widget, it appears on top of all other windows.

Use

The Toplevel widget is extensively used in Tkinter for creating additional windows. It allows creating modal and modeless dialogs, message boxes, and windows for displaying information. The main use of Toplevel widget is to create secondary windows that perform some additional tasks while the main window retains its functionality.

Important Points

  • The Toplevel widget is used to create additional windows.
  • The Toplevel widget appears on top of all other windows.
  • The parent parameter of the Toplevel widget specifies the parent window.
  • The title and geometry methods can be used to set the title and dimensions of the Toplevel widget, respectively.

Summary

In this tutorial, we learned how to use the Toplevel widget in Tkinter. We saw how to create additional windows that can be used for displaying information or creating modal and modeless dialogs. Toplevel is a very useful widget in Tkinter that can create windows that work in conjunction with the main window and provide additional functionality.

Published on: