Python Tkinter – Toplevel Widget

The Toplevel widget is used to create and display the toplevel windows which are directly managed by the window manager. The toplevel widget is used when a python application needs to represent some extra information, pop-up, or the group of widgets on the new window. Toplevel windows have the title bars, borders, and other window decorations. Toplevel widget is used to create a window on top of all other windows.

Syntax:

toplevel = Toplevel(root, bg, fg, bd, height, width, font, Cursor....etc)

Description

root = root window(optional) 
bg = Background colour 
fg = Foreground colour 
bd = Border 
height = Height of the widget. 
width = Width of the widget. 
font = Font type of the text. 
cursor = Cursor that appears on the widget which can be an arrow, a dot etc.

Common methods  

iconify= turns the windows into icon. 
deiconify =turns back the icon into window. 
state= returns the current state of window. 
withdraw= removes the window from the screen. 
title =defines title for window. 
frame= returns a window identifier which is system specific

Example 01.

from tkinter import *
root = Tk()
toplevel = Toplevel()
root.mainloop()

Write Down Code Your Code Editor. To show Your Output. If Use Pycharm Press Shift+F10 and Show Your Output.


Example 02.

from tkinter import *

root = Tk()

root.geometry("500x300")
root.title("ROOT WINDOW")
lbl = Label(root, text="This is root window", font=("Times", 20, "bold"))
top = Toplevel()
top.geometry("400x200")
top.title("TOPLEVEL")
lbl2 = Label(top, text="THIS IS TOP LEVEL WINDOW",font=("Times", 20, "bold"))

lbl.pack(pady=20)
lbl2.pack(pady=20)

top.mainloop()
root.mainloop()

Write Down Code Your Code Editor. To show Your Output. If Use Pycharm Press Shift+F10 and Show Your Output.


Example 03. Top Level Using Button  

from tkinter import *

root = Tk()

root.geometry("800x350+450+86")
root.title("Tkinter Top Level")
root.resizable(width="true", height="true")
root.config(bg="#f0f022")
def windowopentop():
top = Toplevel(root)
top.mainloop()
btn1 = Button(root, text="Window Open", command=windowopentop, font=("Times new roman", 25, "bold"))
btn1.pack(pady=60)


btn2 = Button(root, text="Exit",font=("Times new roman", 25, "bold"), command=root.quit)
btn2.pack(pady=10)
root.mainloop()

Write Down Code Your Code Editor. To show Your Output. If Use Pycharm Press Shift+F10 and Show Your Output.

 


Post a Comment

Previous Post Next Post