Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to center a Tkinter widget in a sticky frame?
Tkinter has lots of inbuilt functions and methods that can be used to configure the properties of Tkinter widgets. These properties vary with different geometry managers. The Grid geometry manager is one of them which deals with many complex layout problems in any application. Grid geometry manager adds all the widgets in the given space without overlapping each other.
Let us suppose that we have created a sticky frame using Grid geometry manager and we want to center the Label text widget inside the frame. In this case, we have to first make the main window sticky by configuring the row and column property. Once the main window gets sticky with the frame, it can make any widget rationally resizable.
Making a Sticky Frame
To center a widget in a sticky frame, we need to configure both the window and frame to be expandable using grid_rowconfigure() and grid_columnconfigure() with weight=1.
Example
# Import the required library
from tkinter import *
# Create an instance of tkinter frame
win = Tk()
# Set the size of the Tkinter window
win.geometry("700x350")
# Add a frame to set the size of the window
frame = Frame(win, relief='sunken', bg='lightgray')
frame.grid(sticky="nsew")
# Make the frame sticky for every case
frame.grid_rowconfigure(0, weight=1)
frame.grid_columnconfigure(0, weight=1)
# Make the window sticky for every case
win.grid_rowconfigure(0, weight=1)
win.grid_columnconfigure(0, weight=1)
# Add a label widget
label = Label(frame, text="Hey Folks! Welcome to Tutorialspoint",
font=('Helvetica', 15, 'bold'), bg="white")
label.grid(row=0, column=0, sticky="")
win.mainloop()
The output of the above code is ?
A window with a centered label inside a sticky frame that expands with the window.
Key Configuration Steps
To properly center a widget in a sticky frame, follow these steps ?
-
Configure window: Use
win.grid_rowconfigure(0, weight=1)andwin.grid_columnconfigure(0, weight=1) -
Configure frame: Use
frame.grid_rowconfigure(0, weight=1)andframe.grid_columnconfigure(0, weight=1) -
Frame placement: Use
frame.grid(sticky="nsew")to make frame expand in all directions -
Widget placement: Place the widget at
row=0, column=0without sticky to center it
Alternative Approach with Sticky Widget
You can also center the widget by making it sticky to all sides ?
from tkinter import *
win = Tk()
win.geometry("700x350")
frame = Frame(win, relief='sunken', bg='lightgray')
frame.grid(sticky="nsew")
# Configure weight for expansion
frame.grid_rowconfigure(0, weight=1)
frame.grid_columnconfigure(0, weight=1)
win.grid_rowconfigure(0, weight=1)
win.grid_columnconfigure(0, weight=1)
# Center label using sticky
label = Label(frame, text="Centered with Sticky",
font=('Helvetica', 15, 'bold'), bg="white")
label.grid(row=0, column=0, sticky="")
win.mainloop()
The label remains centered even when the window is resized.
Conclusion
To center a widget in a sticky frame, configure both the window and frame with weight=1 for rows and columns. Place the frame with sticky="nsew" and the widget at row=0, column=0 without sticky for perfect centering.
