import tkinter as tk
my_w = tk.Tk()
font1=('Times',22,'normal') # font family, size, style
my_w.geometry("400x300") # width and height of the window
l1=['abc','def','ghi','jkl'] # List as source of data
my_row=0 # Starting row number
for data in l1:
my_label=tk.Label(my_w,text=data,font=font1)
my_label.grid(row=my_row,column=0,padx=20,pady=5)
my_row=my_row+1 # Go to Next row by adding 1
my_w.mainloop()
import tkinter as tk
my_w = tk.Tk()
font1=('Times',22,'normal') # font family, size, style
my_w.geometry("400x300") # width and height of the window
l1=['abc','def','ghi','jkl','mno','pkr','frt','qwr','asd','air']
my_row,my_col=0,0 # First row and first column values are set here
for data in l1:
my_label=tk.Label(my_w,text=data,font=font1)
my_label.grid(row=my_row,column=my_col,padx=10,pady=5)
my_col=my_col+1 # Increase column to next on Left side
if(my_col>=3): # Maximum Number of elements in a row reached
my_row=my_row+1 # Go to Next row
my_col=0 # column start from left
my_w.mainloop()

import tkinter as tk
my_w = tk.Tk()
font1=('Times',22,'normal') # font family, size, style
my_w.geometry("400x300") # width and height of the window
l1=['abc','def','ghi','jkl','mno','pkr','frt','qwr','asd','air']
my_row,my_col=0,0
my_labels=[] # to store the reference of all Labels
for data in l1:
my_label=tk.Label(my_w,text=data,font=font1)
my_label.grid(row=my_row,column=my_col,padx=10,pady=5)
my_col=my_col+1 # Increase column to next on Left side
if(my_col>=3): # Maximum Number of elements in a row reached
my_row=my_row+1 # Go to Next row
my_col=0 # column start from left
my_labels.append(my_label)
my_labels[4].config(bg='yellow')#The option is changed using the references
my_w.mainloop()

import tkinter as tk
my_w = tk.Tk()
font1=('Times',22,'normal') # font family, size, style
width,height=400,200
d=str(width)+"x"+str(height)
my_w.geometry(d) # width and height of the window
l1=['abc','def','ghi','jkl','mno','pkr','frt','qwr','asd','air']
my_row,my_col=1,0
for data in l1:
my_label=tk.Label(my_w,text=data,font=font1)
my_label.grid(row=my_row,column=my_col,padx=10,pady=5)
my_col=my_col+1 # Increase column to next on Left side
if(my_col>=3): # Maximum Number of elements in a row reached
my_row=my_row+1 # Go to Next row
my_col=0 # column start from left
height=my_row*50 # calculate new height based on number of rows
d=str(width)+"x"+str(height) # string with new height
my_w.geometry(d) # Update the new dimensions
my_w.mainloop()

import tkinter as tk
my_w = tk.Tk()
font1=('Times',22,'normal') # font family, size, style
width,height=400,200
d=str(width)+"x"+str(height)
my_w.geometry(d) # width and height of the window
l1=[['abc','def','ghi','jkl'],
['mno','pkr','frt','qwr'],
['asd','air','abc','zpq'],
['zae','vbg','qir','zab']]
my_row,my_col=1,0 # First row and first column values are set here
my_labels=[] # to store the reference of all Labels
for data in l1: # get each row of the list as data
for my_col in range(len(data)): # get each element of the row of data
my_label=tk.Label(my_w,text=data[my_col],font=font1)
my_label.grid(row=my_row,column=my_col,padx=10,pady=5)
my_labels.append(my_label) # add the reference
my_row=my_row+1 # Go to Next row
height=my_row*40 # calculate new height based on number of rows
d=str(width)+"x"+str(height) # string with new height
my_w.geometry(d) # Update the dimension of the window
my_labels[4].config(bg='yellow')
my_w.mainloop()
In my loop for creating dynamic labels in Tkinter, I also add ttk separators below each label, enhancing visual separation and clarity in my GUI, especially when displaying data from various sources.
from openpyxl import load_workbook
import tkinter as tk
my_w = tk.Tk()
width,height=450,150 # width and height of the window
d=str(width)+'x'+str(height) # string to set dimension
my_w.geometry(d) # Initial Dimension is set
font1=('Times',18,'normal') # font to be used in Labels
my_row,my_col=1,0
my_labels=[] # to store reference of Labels
wb = load_workbook(filename='E:\\testing\\student.xlsx', read_only=True)
ws = wb['student'] # connecting to sheet
for data in ws.iter_rows(max_col=5,max_row=10,values_only=True):
for my_col in range(len(data)):
my_label=tk.Label(my_w,text=data[my_col],font=font1)
my_label.grid(row=my_row,column=my_col,padx=10,pady=5)
my_labels.append(my_label)
my_row=my_row+1
wb.close()# Close the workbook after reading
height=(my_row-1)*45 # height based on number of rows
d=str(width)+'x'+str(height) # String with new width & height
my_w.geometry(d) #Update window width & height
my_w.mainloop()
from sqlalchemy import create_engine
from sqlalchemy.exc import SQLAlchemyError
my_path="E:\\testing\\sqlite\\my_db.db" #Change the path
my_conn = create_engine("sqlite:///"+ my_path)#SQLite Connection
import tkinter as tk
my_w = tk.Tk()
width,height=450,150 # width and height of the window
d=str(width)+'x'+str(height) # string to set dimension
my_w.geometry(d) # Initial Dimension is set
font1=('Times',18,'normal') # font to be used in Labels
my_row,my_col=1,0
my_labels=[] # to store reference of Labels
try:
r_set=my_conn.execute('SELECT * from student LIMIT 0,2')
except SQLAlchemyError as e:
error=str(e.__dict__['orig'])
print(error)
else:
for data in r_set:
for my_col in range(len(data)):
my_label=tk.Label(my_w,text=data[my_col],font=font1)
my_label.grid(row=my_row,column=my_col,padx=10,pady=5)
my_labels.append(my_label)
my_row=my_row+1
height=(my_row-1)*45 # height based on number of rows
d=str(width)+'x'+str(height) # String with new width & height
my_w.geometry(d) #Update window width & height
my_w.mainloop()
For taking data from MySQL Database, we have to replace the connection line only. Student table SQL dump is here.
#my_path="E:\\testing\\sqlite\\my_db.db" #Change the path
#my_conn = create_engine("sqlite:///"+ my_path)#SQLite Connection
my_conn = create_engine("mysql+mysqldb://userid:pw@localhost/my_db")
Entry Dynamic Buttons
Dynamic Entry
Dynamically managing Spinboxes Label
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.