
total_nos=8
Based on this value we will generate same number of Spinboxes. ( change this value and check how the number of widgets changes. )
sb[i].get(). We can collect all values and display the string inside the Label (lb1).
def sb_show():
my_list=''
for i in range(len(sb)):
my_list=my_list+sb[i].get() + ', '
lb1.config(text=my_list)
def sb_reset():
l1=[]
for i in range(total_nos): # create IntVars
l1.append(tk.IntVar(value=0))
for i in range(len(sb)): # reset all values to 0
sb[i].config(textvariable=l1[i])
lb1.config(text='') # set the value for Label
Full code is here
import tkinter as tk
from tkinter import *
my_w = tk.Tk()
my_w.geometry("400x200")
total_nos=4 # Number of Spinbox to generate
font1=('Times',22,'normal')
font2=('Times',32,'normal')
def sb_reset():
l1=[]
for i in range(total_nos): # create IntVars
l1.append(tk.IntVar(value=0))
for i in range(len(sb)): # reset all values to 0
sb[i].config(textvariable=l1[i])
lb1.config(text='') # set the value for Label
def sb_show():
my_list=''
for i in range(len(sb)):
my_list=my_list+sb[i].get() + ', '
lb1.config(text=my_list)
sb=[] # To store reference to the Spinboxes
for i in range(total_nos):
sbx = Spinbox(my_w,from_=0,to_=5,width=1,font=font1)
sbx.grid(row=0,column=i,padx=3,pady=5)
sb.append(sbx)
b1=tk.Button(my_w,text='Print values',command=sb_show)
b1.grid(row=1,column=0,padx=2,columnspan=2)
b2=tk.Button(my_w,text='Reset',command=sb_reset)
b2.grid(row=1,column=2,padx=3,pady=10)
lb1=tk.Label(text='values here',font=font2) # to show the values
lb1.grid(row=2,column=0,columnspan=total_nos)
my_w.mainloop() # Keep the window open
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.