Tkinter, a GUI module that comes with the Python standard library, is used to create cross-platform GUI apps. This module is simple and lightweight as compared to other GUI modules. Tkinter provides a widget called Toplevel
that is used to create top-level windows. In this answer, we will learn to use the Toplevel
widget.
Toplevel()
Let us take a look at an example.
#import tkinter module import tkinter as tk #create window window = tk.Tk() #provide size to window window.geometry("600x600") #function to create top level widget def display_toplevel(): top = tk.Toplevel(window) top.geometry("300x300") tk.Label(top, text="Hello From Educative !!!").pack() top.mainloop() #button to trigger creation of top level widget tk.Button(window, text = "Click to open top level window", command = display_toplevel).pack() window.mainloop()
Line 5: We create an instance of the Tkinter class Tk()
and assign it to a variable window
.
Line 8: We set the window size as 600x600
.
Line 11-15: We declare and define a function display_toplevel()
to create a top-level widget. In this function, we create a top-level widget, set the widget size, and assign a text label to it.
Line 18: We create a button widget to display a top-level widget when it is clicked. We will call the function display_toplevel
when the button is clicked.
Free Resources