Horje
Center a Label In a Frame of Fixed Size In Tkinter

Tkinter is a module in Python which is used to create GUI applications. It has many useful widgets such as Label, Button, Radiobutton, Checkbutton, Listbox, and more. In this article, we will learn about changing the color of a Tkinter label widget in Python.

Label in Tkinter

The Label is one of the widgets in Tkinter. It is used to display text in the GUI window. An image can also be displayed using the label widget. Also, a label can contain both text and image simultaneously in the window. The text in a Label can be formatted to change its font family, font size, foreground color, background color, font style, and more. A label can be placed anywhere such as left, right, bottom and center.

The syntax for creating a label in Tkinter is as follows:

label_name = tkinter.Label(parent,text="text_to_display")

How to center a Label in a Frame of fixed size in a tkinter?

To center a Label, we can use the option ‘anchor’ in its package manager with value of “CENTER”. Also the option ‘relx’ and ‘rely’ must be set with a value to place the Label in center position of its master container frame relative to the frame size.

Python
# Importing tkinter
import tkinter as tk
# GUI
root = tk.Tk()
root.title("GFG")
root.geometry("300x300")
# Frame of fixed size
frame1 = tk.Frame(root, width=300, height=300)
frame1.grid(row=0, column=0)
# Label within the frame placed at center
label1 = tk.Label(frame1, text="Welcome to GeeksforGeeks",
                  fg="green", font=("Arial", 12, "bold"))
label1.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
root.mainloop()

Output:

center_label_in_frame_of_fixed_size_in_tkinter_pic_1

GUI

Example 2:

Since we know the frame as it is fixed size, we can place the Label at the center by mentioning using ‘x’ option and ‘y’ option. Divide the width of that frame by 2, similarly divide the height of that frame by 2. Here we have created a frame of width 500 and height 500. On dividing 500 by 2, it is 250. Hence, we placed the Label at x coordinate value 250 and same for y coordinate value as 250. Also, the anchor option must have ‘CENTER’ as the value.

Python
import tkinter as tk
# GUI
root = tk.Tk()
root.title("GFG")
root.geometry("500x500")
# Frame of fixed size
frame1 = tk.Frame(root, width=500, height=500)
frame1.grid(row=0, column=0)
# Label within the frame placed at center
label1 = tk.Label(frame1, text="Welcome to GeeksforGeeks",
                  fg="green", font=("Arial", 12, "bold"))
# The width of the frame created here is 500. So, on dividing it by 2, it is 250.
label1.place(x=250, y=250, anchor=tk.CENTER)
root.mainloop()

Output:

center_label_in_frame_of_fixed_size_in_tkinter_pic_2

GUI with Label placed at center of frame of width 500 and height 500

Conclusion:

In this article, we have discussed about how to center a Label in a frame of fixed size in Tkinter.




Reffered: https://www.geeksforgeeks.org


Python

Related
Argparse: Way to include default values in '-help'? Argparse: Way to include default values in '-help'?
Python Pyramid - Application Configuration Python Pyramid - Application Configuration
Building Powerful Telegram Bots with Telethon in Python Building Powerful Telegram Bots with Telethon in Python
Working with multiple environments in Python Poetry Working with multiple environments in Python Poetry
Create a Single Executable from a Python Project Create a Single Executable from a Python Project

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
20