- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Tkinter – How to position a topLevel() widget relative to the root window?
In Tkinter, the toplevel widget is used to create a popup modal window. The popup window created by the toplevel window works similar to the default window of the tkinter application. It can have widgets such as text widget, button widget, canvas widget, frame, etc.
The size and position of the toplevel window can be decided by making it flexible throughout the screen. In the toplevel window, all the widgets are always placed on top of the other windows.
You can use root.winfo_x() and root.winfo_y() to get the position of the root window. Then, you can use the geometry method to position a toplevel widget relative to the root window. Making the toplevel widget relative to the root window prevents the overlapping of the two windows and separates them. Let's take an example to demonstrate how it works.
# Import the required libraries from tkinter import * # Create an instance of tkinter frame or window win = Tk() # Set the size of the window win.geometry("700x300") win.title("Root Window") # Create a toplevel window top = Toplevel(win) top.geometry("400x200") # Create a Label in the toplevel widget Label(top, text= "This is a Toplevel window", font="Calibri, 12").pack() x = win.winfo_x() y = win.winfo_y() top.geometry("+%d+%d" %(x+200,y+200)) # Keep the toplevel window in front of the root window top.wm_transient(win) top.mainloop()
Running the above code will display a toplevel window apart from the main window.
- How to close only the TopLevel window in Python Tkinter?
- How to keep the window focus on the new Toplevel() window in Tkinter?
- How to put a Toplevel window in front of the main window in Tkinter?
- How can I determine the position of a Toplevel in Tkinter?
- How do I get rid of the Python Tkinter root window?
- How can I resize the root window in Tkinter?
- Getting every child widget of a Tkinter window
- How do I position the buttons on a Tkinter window?
- How to set the position of a Tkinter window without setting the dimensions?
- Resize the Tkinter Listbox widget when the window resizes
- Getting the Cursor position in Tkinter Entry widget
- How to update a Python/tkinter label widget?
- How to center a Tkinter widget?
- How to make a Tkinter widget invisible?
- How to connect a variable to the Tkinter Entry widget?