LABELS IN TKINTER
In this tutorial, we are going to learn about the labels in Tkinter. In addition to that, we are going to work with an example to learn about implementing label practically.
What are Labels in Tkinter?
The main purpose of the label widget is to consign information about other widgets. The information used as labels could be specified or optional (e.g. text or image). You can specify your own customized labels in tkinter by modifying them using different features. The syntax of using a label is:
label_tk = Label( window, features )
Label features and properties are:
|1||anchor||It’s shows the initial position of text. It’s initial value is always the CENTER position which is used to position the text in the middle position.|
|2||bg||Displays the background color of the widget.|
|3||bitmap||It is used to display bitmap in the widget but if the image option is already given then this option will be ignored.|
|4||bd||It shows the width of the border. Same as borderwidth|
|5||cursor||Shows the type of cursor when the mouse is hovered over the label. The default is a pointed arrow cursor.|
|6||font||Determines the type of font that is used for the labels.|
|7||fg||Determines the foreground color of font that is used for the labels.|
|8||height||Determines the height of the label widget.|
|9||image||You can use this feature when you want your labels to be used as images.|
|10||justify||If the widget has more than one line then this feature is used to align text in RIGHT, LEFT or CENTER direction.|
|11||padx||Adds padding to the widget in horizontal direction.|
|12||pady||Adds padding to the widget in vertical direction.|
|13||relief||Gives border decoration like FLAT, SUNKEN, RAISED GROOVE and RIDGE. The default border type is FLAT.|
|14||text||This feature determines what will be mentioned as a label;
E.g. text=”Name” where ‘Name’ will be used as a label.
|15||textvariable||By using this feature, you can easily update the text of the label whenever you want.|
|16||underline||It is used with the text option to tell whether the character should be underlined. -1 is a default value (no underline)|
|17||width||Decides the width of the label. If the label is an image then the units will be pixels, if it is text then the units will be text units.|
|18||wraplength||Decides when to wrap the label text into more than one lines.|
Creating Labels Widget
Note: We will use the special notation of tkk for labels, frames to work appropriately.
from tkinter import * from tkinter import messagebox from tkinter import ttk # For labels, frames etc... screen = Tk() screen.geometry('400x400') #creating username label userlabel = ttk.Label(text = "Username").place(x = 30,y = 50) #creating password label password_label = ttk.Label(text = "Password").place(x = 30,y = 80) submit_btn = Button(text = "Submit").place(x = 30,y = 130) #creating username entry username_entry = ttk.Entry(width = 30).place(x = 110, y = 50) #creating password entry password_entry = ttk.Entry(width = 30).place(x = 110, y = 80) screen.mainloop()
Output will be:
References have been used from here