- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I determine the position of a Toplevel in Tkinter?
To place objects in the middle of a frame, we can use the place method. Let's take an example and see how it's done.
Steps −
Import the required libraries and create an instance of tkinter frame.
Set the size of the frame using win.geometry method.
Next, create a button and label it.
Set the position of the buttons using the place method by supplying the x and y coordinate values.
Place the center of the widget at a relative x and y position of 0.5 of button widget (relx=0.5, rely=0.5). Set the anchor at the center by supplying "anchor=CENTER"
Finally, run the mainloop of the application window.
Example
# Import the Tkinter library from tkinter import * from tkinter import ttk # Create an instance of Tkinter frame win = Tk() # Define the geometry win.geometry("750x350") win.title("Main Window") def toplevel_position(): print("The coordinates of Toplevel window are:", top.winfo_x(), top.winfo_y()) top = Toplevel(win, height=150, width=300) top.title("This is the Toplevel Window") top.attributes('-topmost', 'true') button = ttk.Button(top, text="Get position", command=toplevel_position) button.place(relx=0.5, rely=0.5, anchor=CENTER) top.mainloop()
Output
When you execute this code, it will show the following output window −
Now, click the "Get position" button and it will print the coordinates of the Toplevel window on the console.
The coordinates of Toplevel window are: 282 105
- Related Articles
- Python Tkinter – How to position a topLevel() widget relative to the root window?
- How to put a Toplevel window in front of the main window in Tkinter?
- How to close only the TopLevel window in Python Tkinter?
- How do I position the buttons on a Tkinter window?
- How to keep the window focus on the new Toplevel() window in Tkinter?
- How can I disable typing in a ttk.Combobox tkinter?
- How can I dynamically set the position of view in android?
- How can I resize the root window in Tkinter?
- How can I change the text of the Tkinter Listbox item?
- How can I create a simple message box in Tkinter?
- How can I set the row height in Tkinter TreeView?
- How can I determine the connection method used by a MySQL Client?
- How can I manage the start position of searching in MySQL LOCATE() function?
- How can I identify when a Button is released in Tkinter?
- How can I create a dropdown menu from a List in Tkinter?
