![]() |
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 TkinterThe 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.
Output: ![]() 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.
Output: ![]() 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 |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 20 |