How to use the Tkinter label widget
Tkinter is a Python GUI module used to create cross-platform desktop applications. This module comes with Python's standard library, so we don't need to install any modules to use it. It is lightweight and easy to use compared to other GUI modules available. In this answer, we'll learn to create and use a label in Tkinter.
The label widget in Tkinter is used as a box to place images or text inside it.
Syntax
Label(window_instance, options)
Here, we can provide options like background color, foreground color, text, font, height, width, etc.
Example
#import tkinter module
import tkinter as tk
#create window
window = tk.Tk()
#provide size to window
window.geometry("300x300")
#add text label to window
tk.Label(window, text="Hello From Educative !!!"+str(tk.TkVersion), bg = "orange", fg = "white",padx = "20", pady = "20").pack()
window.mainloop()Note: The above program runs on the Tkinter version
8.6
Explanation
Line 5: We create an instance of the Tkinter class
Tk()and assigned it to variablewindow.Line 8: We set the window size as
300x300pixels.Line 11: We create a label widget and attached it to the
windowwith the below options:Foreground color
fgaswhiteBackground-color
bgasorangeText as
Hello From Educative !!!Horizontal padding
padxas20Vertical padding
padyas20
Free Resources