
Tkinter Toplevel in Python
Tkinter Toplevel
In Python’s Tkinter library, the Toplevel widget is used to create and display independent windows that are managed directly by the window manager. These windows can either exist with or without a parent window above them. The Toplevel widget is useful for pop-ups, additional information windows, and grouping related widgets in a separate window.
Toplevel windows come with standard window elements such as title bars, borders, and other decorations provided by the operating system.
Complete Python Course with Advance topics:-Click here
Syntax
The basic syntax for creating a Toplevel window is:
w = Toplevel(options)
Options
The Toplevel widget provides several configuration options to customize the appearance and behavior of the window:
SN | Option | Description |
---|---|---|
1 | bg | Sets the background color of the window. |
2 | bd | Defines the border size of the window. |
3 | cursor | Changes the mouse pointer when hovered over the window. |
4 | class_ | Controls whether selected text is exported to the window manager. Setting it to 0 disables this behavior. |
5 | font | Defines the font type of text inside the widget. |
6 | fg | Sets the foreground color of the window. |
7 | height | Specifies the window height. |
8 | relief | Defines the border style of the window. |
9 | width | Specifies the window width. |
Methods
The Toplevel widget also provides several methods to control its functionality:
SN | Method | Description |
---|---|---|
1 | deiconify() | Displays the window if it was previously minimized. |
2 | frame() | Returns a system-dependent window identifier. |
3 | group(window) | Adds this window to a specific group. |
4 | iconify() | Minimizes the window. |
5 | protocol(name, function) | Assigns a function to a specific protocol event. |
6 | state() | Returns the current state of the window (e.g., normal, iconic, withdrawn). |
7 | transient([master]) | Makes the window a transient (temporary) window. |
8 | withdraw() | Hides the window without destroying it. |
9 | maxsize(width, height) | Sets the maximum window size. |
10 | minsize(width, height) | Sets the minimum window size. |
11 | positionfrom(who) | Specifies the controller for window positioning. |
12 | resizable(width, height) | Controls whether the window can be resized. |
13 | sizefrom(who) | Specifies the controller for window sizing. |
14 | title(string) | Sets the window title. |
Example: Creating a New Window using Toplevel
Here’s a simple example demonstrating how to create a new Toplevel window in Tkinter:
from tkinter import *
root = Tk()
root.geometry("200x200")
def open_window():
new_window = Toplevel(root)
new_window.title("New Window")
new_window.geometry("150x150")
btn = Button(root, text="Open", command=open_window)
btn.place(x=75, y=50)
root.mainloop()
Output:
When the button is clicked, a new pop-up window will appear with a specified title and dimensions.
Download New Real Time Projects :-Click here
Complete Advance AI topics:- CLICK HERE
Conclusion
The Toplevel widget in Tkinter is a powerful tool for managing additional windows in a Python GUI application. Whether you need a pop-up notification, a settings window, or a separate display area, Toplevel provides a simple and effective way to manage multiple windows within your application.
For more Python tutorials and Tkinter guides, stay connected with UpdateGadh!
tkinter toplevel documentation
toplevel tkinter example
tkinter toplevel position
tkinter toplevel always on top
tkinter toplevel modal
tkinter in python
tkinter toplevel geometry
tkinter in python w3schools
tkinter toplevel in python example
Post Comment