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.
Label(window_instance, options)
Here, we can provide options like background color, foreground color, text, font, height, width, etc.
#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
Line 5: We create an instance of the Tkinter class Tk()
and assigned it to variable window
.
Line 8: We set the window size as 300x300
pixels.
Line 11: We create a label widget and attached it to the window
with the below options:
Foreground color fg
as white
Background-color bg
as orange
Text as Hello From Educative !!!
Horizontal padding padx
as 20
Vertical padding pady
as 20
Free Resources