This lesson is about Tkinter Toplevel, a widget in Python’s Tkinter library used to create new top-level windows in GUI applications.
The lesson covers the syntax, options, and methods of Tkinter Toplevel, as well as some examples of its uses. Additionally, the lesson includes multiple-choice quiz questions to test the reader’s understanding of the topic.
This method can be used to minimize (or “iconify”) a Toplevel window.
Here is an example:
import tkinter as tk
# create the main window
root = tk.Tk()
# create a Toplevel window
top = tk.Toplevel(root)
top.title("My Toplevel Window")
# minimize the Toplevel window after 5 seconds
top.after(5000, top.iconify)
# start the main event loop
root.mainloop()

Note: The toplevel window is minimized at taskbar
This method can be used to restore a minimized (or “iconified”) Toplevel window to its original state.
import tkinter as tk
# create the main window
root = tk.Tk()
# create a Toplevel window
top = tk.Toplevel(root)
top.title("My Toplevel Window")
# minimize and then restore the Toplevel window after 5 seconds
top.after(5000, top.iconify)
top.after(7000, top.deiconify)
# start the main event loop
root.mainloop()

This method can be used to set the size and position of a Toplevel window using a string in the format “widthxheight+x+y”.
The width and height are in pixels, and the x and y values specify the position of the top-left corner of the window relative to the screen.
If no argument is provided, the current geometry string is returned.
Here is an example:
import tkinter as tk
# create the main window
root = tk.Tk()
# create a Toplevel window
top = tk.Toplevel(root)
top.title("My Toplevel Window")
# set the geometry of the Toplevel window
top.geometry("300x200+50+50")
# get the current geometry of the Toplevel window
print(top.geometry())
# start the main event loop
root.mainloop()

This method can be used to “grab” the keyboard and mouse events for a Toplevel window, so that all events are directed to that window and its child widgets.
This is useful for creating modal windows or dialogs that require user input.
Here is an example:
import tkinter as tk
# create the main window
root = tk.Tk()
# create a Toplevel window
top = tk.Toplevel(root)
top.title("My Toplevel Window")
# grab the keyboard and mouse events for the Toplevel window
top.grab_set()
# start the main event loop
root.mainloop()

Tkinter Toplevel widgets are used to create secondary windows that appear on top of the main window. They are often used to create dialog boxes, popup windows, and other types of secondary windows that provide additional functionality or information to the user.
Some common use cases for Tkinter Toplevel widgets include:
Dialog boxes: Toplevel windows can be used to create modal or non-modal dialog boxes that prompt the user for input or provide information about the state of the program.
Tool windows: Toplevel windows can be used to create tool windows that provide additional functionality or controls to the user, such as a color picker or a font selector.
Popup windows: Toplevel windows can be used to create popup windows that appear when the user performs a specific action, such as clicking on a button or hovering over a widget.
Multiple document interface (MDI): Toplevel windows can be used to create a multiple document interface, where each document is contained within its own Toplevel window.
Custom windows: Toplevel windows can be used to create custom windows with a specific look and feel, such as a skinned window or a window with a custom border.
Overall, Tkinter Toplevel widgets provide a flexible and powerful way to create secondary windows and enhance the functionality of your Tkinter applications.
import tkinter as tk
from tkinter import colorchooser
# create the main window
root = tk.Tk()
# create a function to show a tool window
def show_tool_window():
# create a Toplevel window for the tool window
tool_window = tk.Toplevel(root)
# add a color picker to the tool window
color_button = tk.Button(tool_window, text="Choose Color",
command=lambda: colorchooser.askcolor())
color_button.pack()
# create a button to show the tool window
button = tk.Button(root, text="Show Tool Window", command=show_tool_window)
button.pack()
# start the main event loop
root.mainloop()

import tkinter as tk
# create the main window
root = tk.Tk()
# create a function to show a popup window
def show_popup(event):
# create a Toplevel window for the popup window
popup = tk.Toplevel(root)
popup.geometry("200x100+{}+{}".format(event.x_root, event.y_root))
# add a label to the popup window
label = tk.Label(popup, text="This is a popup window.")
label.pack()
# create a widget that will trigger the popup window
frame = tk.Frame(root, width=200, height=200)
frame.pack()
frame.bind("<Button-1>", show_popup)
# start the main event loop
root.mainloop()

import tkinter as tk
# create the main window
root = tk.Tk()
# create a function to create a new document window
def new_document():
# create a Toplevel window for the document window
document = tk.Toplevel(root)
# add a label to the document window
label = tk.Label(document, text="This is a new document.")
label.pack()
# create a menu bar with a "File" menu
menu_bar = tk.Menu(root)
root.config(menu=menu_bar)
file_menu = tk.Menu(menu_bar, tearoff=False)
menu_bar.add_cascade(label="File", menu=file_menu)
# add a "New" menu item to the "File" menu
file_menu.add_command(label="New", command=new_document)
# start the main event loop
root.mainloop()

import tkinter as tk
# create the main window
root = tk.Tk()
# create a function to show a child window
def show_child_window():
# create a Toplevel window for the child window
child_window = tk.Toplevel(root)
# set the parent-child relationship between the windows
child_window.transient(root)
# add a label to the child window
label = tk.Label(child_window, text="This is a child window.")
label.pack()
# create a button to show the child window
button = tk.Button(root, text="Show Child Window", command=show_child_window)
button.pack()
# start the main event loop
root.mainloop()

import tkinter as tk
# create the main window
root = tk.Tk()
# create a function to show a modal window
def show_modal_window():
# create a Toplevel window for the modal window
modal_window = tk.Toplevel(root)
# make the modal window modal
modal_window.grab_set()
# add a label to the modal window
label = tk.Label(modal_window, text="This is a modal window.")
label.pack()
# add a button to the modal window to close it
button = tk.Button(modal_window, text="OK", command=modal_window.destroy)
button.pack()
# create a button to show the modal window
button = tk.Button(root, text="Show Modal Window", command=show_modal_window)
button.pack()
# start the main event loop
root.mainloop()

import tkinter as tk
from tkinter import messagebox
# create the main window
root = tk.Tk()
# create a function to show a dialog box
def show_dialog():
# create a Toplevel window for the dialog box
dialog = tk.Toplevel(root)
# add a label to the dialog box
label = tk.Label(dialog, text="This is a dialog box.")
label.pack()
# add a button to the dialog box
button = tk.Button(dialog, text="OK", command=dialog.destroy)
button.pack()
# make the dialog box modal
dialog.grab_set()
# create a button to show the dialog box
button = tk.Button(root, text="Show Dialog Box", command=show_dialog)
button.pack()
# start the main event loop
root.mainloop()

import tkinter as tk
import tkinter.messagebox as messagebox
# create the main window
root = tk.Tk()
# create a function to show a dialog box
def show_dialog_box():
# create a Toplevel window for the dialog box
dialog_box = tk.Toplevel(root)
# make the dialog box modal
dialog_box.grab_set()
# add a label to the dialog box
label = tk.Label(dialog_box, text="Are you sure you want to quit?")
label.pack(pady=10)
# add buttons to the dialog box to confirm or cancel
button_frame = tk.Frame(dialog_box)
button_frame.pack(pady=10)
confirm_button = tk.Button(button_frame, text="Yes", command=root.quit)
confirm_button.pack(side="left", padx=10)
cancel_button = tk.Button(button_frame, text="No", command=dialog_box.destroy)
cancel_button.pack(side="left", padx=10)
# create a button to show the dialog box
button = tk.Button(root, text="Quit", command=show_dialog_box)
button.pack()
# start the main event loop
root.mainloop()

import tkinter as tk
import tkinter.ttk as ttk
# create the main window
root = tk.Tk()
# create a function to show a child window
def show_child_window():
# create a Toplevel window for the child window
child_window = tk.Toplevel(root)
# set the parent-child relationship between the windows
child_window.transient(root)
# create a notebook widget in the child window
notebook = ttk.Notebook(child_window)
notebook.pack(fill="both", expand=True)
# add some tabs to the notebook
for i in range(1, 6):
tab = tk.Frame(notebook)
notebook.add(tab, text="Tab {}".format(i))
# create a menu bar
menu_bar = tk.Menu(root)
root.config(menu=menu_bar)
# create a "File" menu
file_menu = tk.Menu(menu_bar, tearoff=False)
menu_bar.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="New", command=show_child_window)
file_menu.add_command(label="Open")
file_menu.add_separator()
file_menu.add_command(label="Exit", command=root.quit)
# start the main event loop
root.mainloop()

import tkinter as tk
# create the main window
root = tk.Tk()
# create a function to show a custom-styled window
def show_custom_window():
# create a Toplevel window for the custom window
custom_window = tk.Toplevel(root)
# set the background color of the custom window
custom_window.config(bg="#0066cc")
# set the window title and icon
custom_window.title("Custom Window")
custom_window.iconbitmap("custom_icon.ico")
# set the window size and position
custom_window.geometry("300x200+{}+{}".format(
int(root.winfo_screenwidth()/2 - 150),
int(root.winfo_screenheight()/2 - 100)))
# create a button to show the custom window
button = tk.Button(root, text="Show Custom Window", command=show_custom_window)
button.pack()
# start the main event loop
root.mainloop()

1-What is a Tkinter Toplevel widget?
a) A widget that allows you to add images to your GUI application
b) A widget that allows you to create a new window in your GUI application
c) A widget that allows you to add text to your GUI application
d) A widget that allows you to create a dropdown menu in your GUI application
Answer: b
2-What is the syntax for creating a new Tkinter Toplevel window?
a) top = Toplevel(root)
b) top = Toplevel()
c) top = Toplevel(parent)
d) top = Toplevel(master)
Answer: c
3-Which option can be used to set the title of a Tkinter Toplevel window?
a) title
b) text
c) name
d) label
Answer: a
4-Which method can be used to destroy a Tkinter Toplevel window?
a) destroy()
b) delete()
c) remove()
d) close()
Answer: a
5-Which option can be used to make a Tkinter Toplevel window modal?
a) modal
b) topmost
c) grab
d) focus
Answer: c
6-Which option can be used to set the size of a Tkinter Toplevel window?
a) size
b) dimensions
c) geometry
d) extent
Answer: c
7-Which method can be used to set the parent-child relationship between two Tkinter windows?
a) set_parent()
b) set_child()
c) transient()
d) associate()
Answer: c
8-Which option can be used to set the icon of a Tkinter Toplevel window?
a) icon
b) iconbitmap
c) iconimage
d) iconfile
Answer: b
9-Which method can be used to make a Tkinter Toplevel window resizable?
Answer: b
10-Which use case of Tkinter Toplevel involves creating a child window within a parent window?
Answer: c
11-Which option can be used to set the background color of a Tkinter Toplevel window?
Answer: a
12-Which method can be used to center a Tkinter Toplevel window on the screen?
Answer: b
13-Which option can be used to set the position of a Tkinter Toplevel window on the screen?
Answer: c
14-Which method can be used to set the minimum size of a Tkinter Toplevel window?
Answer: a
15-Which option can be used to set the maximum size of a Tkinter Toplevel window?
Answer: a
16-Which method can be used to hide a Tkinter Toplevel window?
Answer: b
17-Which option can be used to set the border width of a Tkinter Toplevel window?
Answer: b
18-Which method can be used to get the current size of a Tkinter Toplevel window?
Answer: c
19-Which option can be used to set the icon of a Tkinter Toplevel window using an image file?
Answer: b
20-Which method can be used to get the current position of a Tkinter Toplevel window?
Answer: c
21-Which option can be used to set the title of a Tkinter Toplevel window?
Answer: a
22-Which method can be used to set the focus on a Tkinter Toplevel window?
Answer: c
23-Which option can be used to set the style of the window border of a Tkinter Toplevel window?
Answer: c
24-Which method can be used to resize a Tkinter Toplevel window to fit its contents?
Answer: b
25-Which option can be used to set the transparency level of a Tkinter Toplevel window?
Answer: b
26-Which method can be used to prevent a Tkinter Toplevel window from being resized?
Answer: b
27-Which option can be used to set the initial size of a Tkinter Toplevel window?
Answer: b
28-Which method can be used to move a Tkinter Toplevel window to a specific position on the screen?
Answer: b
29-Which option can be used to set the icon of a Tkinter Toplevel window using a bitmap file?
Answer: b
30-Which method can be used to display a Tkinter Toplevel window as a modal dialog?
Answer: d
31-Which option can be used to set the background color of a Tkinter Toplevel window?
Answer: b
32-Which method can be used to destroy a Tkinter Toplevel window?
Answer: a
33-Which option can be used to set the position of a Tkinter Toplevel window relative to its parent window?
Answer: d
34-Which method can be used to set the minimum and maximum size of a Tkinter Toplevel window?
Answer: b
35-Which option can be used to prevent a Tkinter Toplevel window from being moved by the user?
Answer: d
36-Which method can be used to retrieve the current position of a Tkinter Toplevel window?
Answer: b
37-Which option can be used to set the icon of a Tkinter Toplevel window using an image file?
Answer: b
38-Which method can be used to center a Tkinter Toplevel window on the screen?
Answer: c
39-Which option can be used to make a Tkinter Toplevel window stay on top of other windows?
Answer: b
40-Which method can be used to hide a Tkinter Toplevel window?
Answer: b
Here are some references used for creating this lesson:
Tkinter Toplevel documentation:
Python GUI Programming with Tkinter: