import tkinter as tk
my_w=tk.Tk()
my_w.geometry('300x100')
my_w.title('www.plus2net.com')
l3 = tk.Label(my_w, text='Welcome', width=15 )
l3.grid(row=1,column=1)
my_w.mainloop()
my_font1=('times', 18, 'bold') # font style declaring
l3 = tk.Label(my_w, text='Welcome', width=15,font=my_font1,fg='red' )
l3 = tk.Label(my_w, text='Welcome', width=15,font=my_font1,
fg='red',bg='yellow' )
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("500x500")
my_str = tk.StringVar()
l1 = tk.Label(my_w, textvariable=my_str, width=10 )
l1.grid(row=1,column=2)
my_str.set("Hi Welcome")
my_w.mainloop()
Here by using set() method the StringVar() is assigned Hi Welcome, this can be updated to any other value to reflect the change in text of the Label.
import tkinter as tk
my_w=tk.Tk()
my_w.geometry('300x100')
my_w.title('www.plus2net.com')
f1=("Times", 22, 'overstrike')
l3 = tk.Label(my_w,fg='green',text='strikethrough text word',font=f1)
l3.grid(row=1,column=1,padx=2)
my_w.mainloop()
We can add other styles.
f1=("Times", 22, ('overstrike','italic','bold'))
n, ne, e, se, s, sw, w, nw, or center
. These values are based on four direction North , South, East and West.
import tkinter as tk
from tkinter import ttk
my_w = tk.Tk()
my_w.geometry("400x250") # Size of the window
my_w.title("www.plus2net.com") # title
l1=tk.Label(my_w,text='plus2net',bg='yellow',
height=13,width=40,anchor='center')
l1.grid(row=0,column=0,padx=20,pady=20)
my_w.mainloop() # Keep the window open
import tkinter as tk
from tkinter import ttk
my_w = tk.Tk()
my_w.geometry("400x250") # Size of the window
my_w.title("www.plus2net.com") # title
font1=('Times',22,'normal')
l1=tk.Label(my_w,text='plus2net',bg='yellow',font=font1,
height=5,width=20,anchor='center')
l1.grid(row=0,column=0,columnspan=9,padx=20,pady=20)
r1_v = tk.StringVar() # string variable
r1_v.set('center') # Can assign value Appear or Failed
def my_upd():
l1.config(anchor=r1_v.get())
r1 = tk.Radiobutton(my_w, text='n', variable=r1_v, value='n',command=my_upd)
r1.grid(row=1,column=0)
r2 = tk.Radiobutton(my_w, text='ne', variable=r1_v, value='ne',command=my_upd)
r2.grid(row=1,column=1)
r3 = tk.Radiobutton(my_w, text='e', variable=r1_v, value='e',command=my_upd)
r3.grid(row=1,column=2)
r4 = tk.Radiobutton(my_w, text='se', variable=r1_v, value='se',command=my_upd)
r4.grid(row=1,column=3)
r5 = tk.Radiobutton(my_w, text='center', variable=r1_v, value='center',command=my_upd)
r5.grid(row=1,column=4)
r6 = tk.Radiobutton(my_w, text='s', variable=r1_v, value='s',command=my_upd)
r6.grid(row=1,column=5)
r7 = tk.Radiobutton(my_w, text='sw', variable=r1_v, value='sw',command=my_upd)
r7.grid(row=1,column=6)
r8 = tk.Radiobutton(my_w, text='w', variable=r1_v, value='w',command=my_upd)
r8.grid(row=1,column=7)
r9 = tk.Radiobutton(my_w, text='nw', variable=r1_v, value='nw',command=my_upd)
r9.grid(row=1,column=8)
my_w.mainloop() # Keep the window open
lb1 = tk.Label(my_w, text='Welcome \n 2nd line \n third', height=4,
anchor='w',justify='left',width=60,bg='lightgreen' )
lb1.grid(row=2,column=1,padx=10,columnspan=3,sticky='w')
anchor='e'
to align the Label text close to the Entry widget.
l1=tk.Label(my_w,text='plus2net',bg='lightgreen',
width=20,anchor="e")
l1.grid(row=0,column=0,padx=2,pady=20)
e1 = tk.Entry(my_w,width=20,bg='yellow')
e1.grid(row=0,column=1)
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("400x250") # Size of the window
my_w.title("www.plus2net.com") # title
font1=('Times',22,'normal') # font style and size
str1=tk.StringVar(value='plus2net') # common string Variable
l1=tk.Label(my_w,bg='lightgreen',textvariable=str1,width=10,font=font1)
l1.grid(row=0,column=0,padx=5,pady=20)
e1 = tk.Entry(my_w,width=10,bg='yellow',font=font1,textvariable=str1)
e1.grid(row=0,column=1)
my_w.mainloop() # Keep the window open
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("400x250") # Size of the window
my_w.title("www.plus2net.com") # title
font1=('Times',22,'normal') # font style and size
str1=tk.StringVar(value='plus2net') # common string Variable
lb1=tk.Label(my_w,bg='lightgreen',textvariable=str1,width=10,font=font1)
lb1.grid(row=0,column=0,padx=5,pady=20,columnspan=2)
def my_upd(my_msg):
lb1['bg']='lightyellow'
str1.set(my_msg) # update to new message
lb1.after(3000,lambda:str1.set('plus2net'))
lb1.after(3000,lambda:lb1.config(bg='lightgreen'))
bt1 = tk.Button(my_w,width=10,bg='yellow',
font=font1,text='Update 1',command=lambda:my_upd('Data updated'))
bt1.grid(row=1,column=0,padx=15,pady=15)
bt2 = tk.Button(my_w,width=10,bg='yellow',
font=font1,text='Delete 2',command=lambda:my_upd('Data deleted'))
bt2.grid(row=1,column=1)
my_w.mainloop() # Keep the window open
import tkinter as tk
my_w = tk.Tk()
import random
my_w.geometry("300x200") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
my_list=['Alex','Ron','Ravi','Geek','Rbindra']
lb1=tk.Label(my_w,text='plus2net',width=15,bg='yellow',font=22)
lb1.grid(row=0,column=0,padx=30,pady=50)
def my_fun():
random_element = random.choice(my_list) # random selection
lb1.config(text=random_element) # update the text
my_w.after(1000, my_fun) # call the function after a delay
my_fun()
my_w.mainloop() # Keep the window open
b1 = tk.Button(my_w,text='Delete',command=lambda:l1.destroy())
b1.grid(row=0,column=0,padx=10,pady=10)
l1=tk.Label(my_w,bg='lightgreen',text='Welcome')
l1.grid(row=1,column=0,padx=5,pady=10)
We can hide ( remove ) and restore (display ) the Label by using grid_remove() and restore by grid().
import tkinter as tk
my_w=tk.Tk()
my_w.geometry('320x120')
my_w.title('www.plus2net.com')
my_font1=('times', 14) # font style declaring
l0 = tk.Label(my_w, text='relief', width=10)
l0.grid(row=1,column=2)
l1 = tk.Label(my_w, text='raised', width=10,font=my_font1,
fg='red',borderwidth=2, relief='raised' )
l1.grid(row=2,column=2)
l2 = tk.Label(my_w, text='sunken', width=10,font=my_font1,
fg='red',borderwidth=2, relief='sunken' )
l2.grid(row=2,column=3)
l3 = tk.Label(my_w, text='ridge', width=10,font=my_font1,
fg='red',borderwidth=2, relief='ridge' )
l3.grid(row=3,column=2)
l4 = tk.Label(my_w, text='solid', width=10,font=my_font1,
fg='red',borderwidth=2, relief='solid' )
l4.grid(row=3,column=3)
l5 = tk.Label(my_w, text='groove', width=10,font=my_font1,
fg='red',borderwidth=2, relief='groove' )
l5.grid(row=4,column=2)
l6 = tk.Label(my_w, text='flat', width=10,font=my_font1,
fg='red',borderwidth=2, relief='flat' )
l6.grid(row=4,column=3)
my_w.mainloop()
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("300x200")
my_font1=('times', 18, 'bold')
my_str1 = tk.StringVar()
l1 = tk.Label(my_w, textvariable=my_str1, fg='red',font=my_font1 )
l1.grid(row=1,column=2,columnspan=3,sticky='E')
my_str1.set("plus2net.com")
my_font2=('Verdana', 10, 'normal')
my_str2 = tk.StringVar()
l2 = tk.Label(my_w, textvariable=my_str2, fg='black',font=my_font2 )
l2.grid(row=2,column=1)
my_str2.set("python tutorials")
my_font3=('MS Sans Serif', 10, 'italic')
my_str3 = tk.StringVar()
l3 = tk.Label(my_w, textvariable=my_str3, fg='green',font=my_font3 )
l3.grid(row=3,column=3)
my_str3.set("Tkinter")
my_str4 = tk.StringVar()
l4 = tk.Label(my_w, textvariable=my_str4, font=my_font2 )
l4.config(fg='blue')
l4.grid(row=4,column=4)
my_str4.set("Pandas")
my_str5 = tk.StringVar()
l5 = tk.Label(my_w, textvariable=my_str5, fg='yellow',font=my_font2 )
l5.grid(row=5,column=5)
my_str5.set("Numpy")
my_w.mainloop()
There are many optional options we can add to Label, the list is available below.# Change the text of a label
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("500x500")
def my_upd():
my_str.set("plus2net.com")
my_str = tk.StringVar()
l1 = tk.Label(my_w, textvariable=my_str, width=10 )
l1.grid(row=1,column=2)
my_str.set("Hi Welcome")
b1 = tk.Button(my_w, text='Change text', width=15,bg='yellow',command=lambda: my_upd())
b1.grid(row=2,column=2)
my_w.mainloop()
Read text from Label and copy the same to another Label
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("500x500")
def my_upd():
my_str.set("plus2net.com") # set string for button 1
my_copy_text=l1.cget('text')
my_str2.set(my_copy_text) # read text from button 1 and set text to button 2
#my_str = tk.StringVar()
l1 = tk.Label(my_w, text='Hi Welcome', width=10 )
l1.grid(row=1,column=2)
#my_str.set("Hi Welcome")
my_str2 = tk.StringVar()
l2 = tk.Label(my_w, textvariable=my_str2, width=10 )
l2.grid(row=1,column=3)
my_str2.set("I will change")
b1 = tk.Button(my_w, text='Change text', width=15,bg='yellow',command=lambda: my_upd())
b1.grid(row=2,column=2)
my_w.mainloop()
l1.config(fg='green') # foreground color
Like this many other values we can change.
l1.config(bg='yellow') # background color
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("400x200")
l1 = tk.Label(my_w, width=15 )
l1.grid(row=1,column=1)
my_img = tk.PhotoImage(file = "D:\\top2.png")
l2 = tk.Label(my_w, image=my_img )
l2.grid(row=1,column=2)
my_w.mainloop()
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("250x200")
l1 = tk.Label(my_w, width=15 )
l1.grid(row=1,column=1,pady=20)
l2 = tk.Label(my_w, bitmap='question' )
l2.grid(row=1,column=2)
my_w.mainloop()
Other bitmaps are 'error', 'gray75', 'gray50', 'gray25', 'gray12', 'hourglass', 'info', 'questhead', 'question', and 'warning'l1 = tk.Label(my_w, text='Disabled',state='disabled')
l1.grid(row=1,column=1)
l2 = tk.Label(my_w, text='normal',state='normal')
l2.grid(row=1,column=2)
l3 = tk.Label(my_w, text='active',state='active')
l3.grid(row=1,column=3)
By using config the state can be managed.
l1.config(state='normal')
What is the difference between normal and active ? import tkinter as tk
from tkinter import *
my_w = tk.Tk()
my_w.geometry("300x150") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
def my_fun(*args):
my_w_child=Toplevel(my_w) # Child window
my_w_child.geometry("200x200") # Size of the window
l1=tk.Label(my_w,text='I am')
l1.grid(row=0,column=0,padx=2,pady=10)
l2=tk.Label(my_w,text='OK',fg='red')
l2.grid(row=0,column=1,padx=2,pady=10)
l2.bind("<Button-1>",my_fun) # mouse click
my_w.mainloop() # Keep the window open
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("620x590")
bg='#ffffff'
l0=tk.Label(my_w,text=' ')
l0.grid(row=0,column=0)
height=4
width=10 # change this to match your display
for i in range (1,9):
if bg=='#ffffff':
bg='#000000'
else:
bg='#ffffff'
for j in range(1,9):
if bg=='#ffffff':
bg='#000000'
else:
bg='#ffffff'
l1=tk.Label(my_w,bg=bg,height=height,width=width,
borderwidth=1,relief='groove')
l1.grid(row=i,column=j)
my_w.mainloop()
import webbrowser
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("400x200")
my_link = tk.Label(my_w, text="plus2net Hyperlink",
fg="blue", cursor="hand2",font=['Times',22,'underline'])
my_link.grid(row=0,column=0,padx=20,pady=20)
my_link.bind("<Button-1>",
lambda e: webbrowser.open_new("https://www.plus2net.com"))
my_w.mainloop()
import webbrowser
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("400x200")
def my_open():
url = "https://www.plus2net.com"
webbrowser.open_new(url)
my_button = tk.Button(my_w,text="plus2net", fg="blue",
cursor="hand2",font=18,command=my_open)
my_button.grid(row=1, column=1,padx=20, pady=20)
my_w.mainloop()
import webbrowser
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("400x200")
l1=tk.Label(my_w,text='Name',font=22)
l1.grid(row=0,column=0,padx=20,pady=20)
e1 = tk.Entry(my_w, font=22)
e1.grid(row=0, column=1, padx=5, pady=20)
my_link = tk.Label(
my_w,
text="plus2net Hyperlink",
fg="blue",
cursor="hand2",
font=["Times", 22, "underline"],
)
my_link.grid(row=1, column=0, columnspan=2,padx=20, pady=20)
def my_open():
url = "https://www.plus2net.com/php_tutorial/form-get.php?name=" + e1.get()
webbrowser.open_new(url)
my_link.bind("<Button-1>", lambda e: my_open())
my_w.mainloop()
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.