We can use anchor attribute to align the text within a Label. Here we have given fixed width and height by using height=13 and width =40 and background colour='yellow' to give more space for different alignments using anchor option.
Default value for anchor is 'center'.
Other values for anchor are 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
We can align text in all 9 directions by using 9 radioButtons. Here we are updating the anchor attribute by using config() method.
For any entry widget we can use Label to indicate the type of data required. Here is one with anchor='e' to align the Label text close to the Entry widget.
We will use one Entry widget to take the user input and update the text of the Label using the user entered data. Here we are connecting both the widgets to a common StringVar by using textvariable option.
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
Each button on click calls the function my_upd() with a message as parameter.
Inside the function my_upd() after displaying the message, by using after() we are restoring to original message after a time delay of 3000 milliseconds.
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
Randomly displaying text from a list with a time delay ↑
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
We can delete ( permanently ) a Label from Tkiner window by using destroy(). Here we have used one button and onClick event of the button the Label is deleted.
To manage layout read more on grid() →
We will change the font size, font colour, font family and place them at different locations using GRID row and column layouts.
Tkinter bind click event of a Label to open Toplevel child window on bind
There is no command option for label. We can use bind option to trigger function on click of a Label. Here one child window is opened on click of the Label saying OK.
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()
We will create the URL by asking user to enter name in one Entry box. On click of the link the site and particular page will open and it will display the welcome message by using the entered name.
Using user input data of Tkinter to Pass to URL and opening web page by webbrowser
Creating Dynamic Row-Wise Tkinter Labels from Data Sources ↑
Creating dynamic Tkinter labels in a loop, especially when dealing with multiple labels sourced from a database or various data sources, involves iterating through the data and generating a label for each entry. This process is crucial for displaying data row-wise, where each piece of data gets its own label on the GUI.
Dynamic Label
By using four buttons change the Text and Background colour of a Label. On clicking the first button the label should change to Google and background colour should change to Red. Similarly the text and background colour should change on clicking for all other three buttons.