- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 to place objects in the middle of a frame using 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") # Create Buttons in the frame button = ttk.Button(win, text="Button at the Center") button.place(relx=0.5, rely=0.5, anchor=CENTER) win.mainloop()
Output
When you execute this code, it will show the following output window −
Now, try resizing the window and you will notice that the button widget automatically centers itself accordingly.
- Related Articles
- How to place an image into a frame in Tkinter?
- How to use images in Tkinter using photoimage objects?
- How to temporarily remove a Tkinter widget without using just .place?
- How to clear out a frame in the Tkinter?
- How to change the width of a Frame dynamically in Tkinter?
- How do I change the background of a Frame in Tkinter?
- How to center a Tkinter widget in a sticky frame?
- How to gray out (disable) a Tkinter Frame?
- How to center a label in a frame of fixed size in Tkinter?
- How to Change Tkinter Frame Title?
- Using a Frame class in a Tk class in Python tkinter
- How to put a border around a Frame in Python Tkinter?
- How to place the text at the center of an Entry box in Tkinter?
- How to set the min and max height or width of a Frame in Tkinter?
- How to change the font and size of buttons and frame in tkinter?
