How do I create child windows with Python tkinter?

TkinterServer Side ProgrammingProgramming

The child window can be referred to as the independent window which is separated from the root or main window. In order to create a child window, we have to define a toplevel window which can be created manually using the Toplevel(win) method. In the method toplevel(root), we have to pass the main window as the parameter and further define the widgets if needed.

Example

Let us create a child window which contains some widgets in it.

#Import tkinter library
from tkinter import *
from tkinter import ttk
#Create an instance of tkinter frame
win = Tk()
#Set the geometry and title of tkinter Main window
win.geometry("750x250")
win.title("Main Window")
#Create a child window using Toplevel method
child_w= Toplevel(win)
child_w.geometry("750x250")
child_w.title("New Child Window")
#Create Label in Mainwindow and Childwindow
label_main= Label(win, text="Hi, this is Main window", font=('Helvetica 15'))
label_main.pack(pady=20)
label_child= Label(child_w, text= "Hi, this is Child Window", font=('Helvetica 15'))
label_child.pack()
win.mainloop()

Output

When we run the above code, it will display two windows: a Main window and a Child Window

Main Window

Child Window

raja
Published on 15-Apr-2021 17:11:09
Advertisements