- 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 center a label in a frame of fixed size in Tkinter?
Tkinter is a GUI toolkit in Python used to build desktopbased applications. Tkinter provides several widget functionalities and class libraries to develop various components of an application. The Frame widget is one of the widgets that works similar to the standard tkinter default window. You can place as many widgets as you want in a Frame widget. You can also customize the properties like resizing the frame, its background color and also the layout using geometry managers.
Example
Suppose we need to create an application in which we want to create a Label widget inside a fixedsize frame. The Label widget must be placed at the center and to achieve this, we can use the anchor=CENTER property of the place geometry manager. The following example demonstrates how to implement it.
# Import the library from tkinter import * from tkinter import filedialog # Create an instance of window win=Tk() # Set the geometry of the window win.geometry("700x350") # Create a frame widget frame=Frame(win, width=300, height=300) frame.grid(row=0, column=0, sticky="NW") # Create a label widget label=Label(win, text="I am inside a Frame", font='Arial 17 bold') label.place(relx=0.5, rely=0.5, anchor=CENTER) win.mainloop()
Output
Running the above code will display a window with a centered Label widget inside a Frame.
- Related Articles
- How to center a Tkinter widget in a sticky frame?
- How to change the size of text on a label in Tkinter?
- Python Tkinter – How do I change the text size in a label widget?
- How to center a Tkinter widget?
- How to create a hyperlink with a Label in Tkinter?
- How to change the font and size of buttons and frame in tkinter?
- How to horizontally center a widget using a grid() in Tkinter?
- How to center a window on the screen in Tkinter?
- Deleting a Label in Python Tkinter
- How to set the height/width of a Label widget in Tkinter?
- How to update a Python/tkinter label widget?
- How to add Label width in Tkinter?
- How to clear out a frame in the Tkinter?
- How to change the color of a Tkinter label programmatically?
- How to update the image of a Tkinter Label widget?
