Signup/Sign In

Python Tkinter Toplevel Widget

In this tutorial, we will cover the Tkinter Toplevel widget in Python which is used to create and display top-level windows other than the application window.

  • With the help of the Tkinter Toplevel widget, you can provide extra information to the user in a separate window on top of the parent window.

  • This top-level window created using the Toplevel widget is directly organized and managed by the window manager.

  • It is not necessary for the top-level windows to have parents on their top.

  • You can create multiple top-level windows one over the other.

  • Top-level windows created using Top-level widgets contain title bars, borders, and some window decorations too.

  • With the help of this widget, you can provide pop-ups, some extra information, or some widgets on the new window if you want.

Python Tkinter Toplevel Widget

The syntax of the Tkinter Toplevel widget is given below:

W = Toplevel(master,options) 

In the above syntax, the master parameter denotes the parent window. You can use many options to configure your Toplevel widget and these options are written as comma-separated key-value pairs.

Tkinter Toplevel Widget Options:

Following are the various options used with Tkinter Toplevel widgets are given below:

Name of the option Description
bd To represent the border size of the window
bg To represent the background color of the window
class_ Generally, the text selected in the text widget is simply exported to be selected to the window manager. You can also set the value of this option to 0 to make this kind of behavior false.
cursor This option will convert the mouse pointer to the specified cursor type and it can be set to an arrow, dot, etc.
width This option is used to represent the width of the window
height This option is used to represent the height of the window
font This option indicates the font type of the text to be inserted into the widget.
fg This option is used to indicate the foreground color of the widget.
relief This option indicates the type of the window.

Tkinter Toplevel Widget Methods:

Following are the various methods used with Tkinter Toplevel widgets are given below:

Method Description
title(string) This method is used to define the title for the window.
withdraw() This method is used to delete the window but it would not destroy the window.
positionfrom(who) This method is used to define the position controller
sizefrom(who) This method is used to define the size controller.
minsize(width,height) This method is used to declare the minimum size for the window
maxsize(width,height) This method is used to declare the maximum size for the window
resizable(width,height) This method is used to control whether the window can be resizable or not.
transient([master]) This method is used to convert the window into a temporary window
iconify() This method is used to convert the top-level window into an icon.
deiconify() This method is mainly used to display the window.
frame() To indicate a system-dependent window identifier this method is used.
group(window) This method is used to add a top-level window to a specified window group
protocol(name,function) This method is used to indicate a function which will be called for the specific protocol
state() This method is used to get the current state of the window. Some Possible values of this option are normal, iconic, withdrawn, and icon.

Tkinter Toplevel Widget Example

Below we have a basic example where we will create a simple top-level window.

from tkinter import *  
win = Tk()  
def open():  
    top = Toplevel(win)  
btn = Button(win, text="open", command=open), y=50)  

In the code above, we have created a Toplevel widget that is created and started when the Button is clicked.


So now we know what a Tkinter Toplevel widget is and how to create it. The Tkinter Toplevel widget is good to show some section of your application in a different window which is displayed on the top of the main application window.

About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight