Dynamically managing Spinboxes

Tkinter Spinbox
Dynamically created Spinbox

We are not sure of number of Spinboxes required and based on any output from database or from other counts, we can create Spinbox widgets dynamically and manage them.

Here we have assigned value to one variable total_nos
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. )

This variable total_nos is used inside the script to access all Spinboxes and the options.

Tkinter generating Spinboxes dynamically and reading the values and resetting textvariable option

Read and display all set values of Spinboxes

The set value of each Spinbox we can get by using the index position as reference to the Spinbox, like this 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)

Resetting the set values

We can reset all Spinboxes by managing the option textvariable. Here we can't directly assing the value to textvariable option of Spinbox and use one set ( list ) of IntVar() to set the values for textvariable.
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

MySQL , SQLite,CSV and Json data is used to add options for Spinbox
Spinbox Dynamically Managing Buttons Dynamic Entry widgets
Subscribe to our YouTube Channel here

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2022 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer