Dynamically managing Entry & Label



Tkinter dynamically creating and validating Entry and Label widgets on user inputs and messaging


We are not sure how many Entry widgets we are going to use as our requirement may change.

Example : We want to enter the marks for all the students where details are stored in a database. Here number of students is not constant, so number of entry widgets required is not fixed.

While submitting all the entries the user has to fill all the entry boxes ( minimum 3 chars ).

Here we have to use dynamic creation of label and entry widgets and by using their references we will manage them.

Let us start from a basic layout of our Tkinter window.
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("400x250")  # Size of the window 
my_w.title("www.plus2net.com")  #  title
my_w.mainloop()  # Keep the window open
Number of inputs required can change, here we are keeping one variable and value of this can be changed to dynamically create and manage the Entry widgets along with the Label. Note that this requirement ( value ) can come from different sources.
number_of_inputs=5 # Number of entries required 
To store the references of the Entry widgets we will create one blank list. To this list we will add reference of our Entry widgets by using list append method.
ref=[] # to store the references 
for j in range(number_of_inputs):
    l=tk.Label(my_w,text='Entry: '+str(j+1),font=20,fg='blue')
    l.grid(row=j,column=0,padx=3)

    e = tk.Entry(my_w, font=20,bg='lightyellow') 
    e.grid(row=j, column=1,padx=10,pady=3) 

    ref.append(e) # store references 

Submit button and Message

We will use one button to check the entries and one Label to show the message to users.

Here on click of the button the the function my_check() is triggered.
The grid row value for the button is taken based on the value of variable j in above code.
b1=tk.Button(my_w,text='Submit',
 bg='lightgreen',command=lambda: my_check(),font=18)
b1.grid(row=j+1,column=0,padx=3,pady=5)

l1=tk.Label(my_w,text='Output here',font=20) # display message
l1.grid(row=j+1,column=1)
On click of the Submit button the function my_check() is executed and first we set a flag my_flag=True. Based on the final value of this flag we will show the matching message using the label l1.

By using the reference list ref we will check all the entries and the data we will read by using get() method. The length of each entry is checked by using len() function.

If the input data is not more than 3 char then the flag my_flag is set to True.

After checking all the inputs, based on the value of the flag ( my_flag ) the matching message is displayed using config().

The message is hidden after displaying for 3 seconds ( 3000 milliseconds ) by using after().
def my_check():
    my_flag=False
    for w in ref:
        if(len(w.get())<3):
            #print(w.get())
            my_flag=True
    if(my_flag==False):
        l1.config(text="Form can be submitted",fg='green')
    else:
        l1.config(text="Fill all the entries",fg='red' )
    l1.after(3000, lambda: l1.config(text=''))
Full code is here
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("400x250")  # Size of the window 
my_w.title("www.plus2net.com")  #  title
number_of_inputs=2 # Number of entries required 
ref=[] # to store the references 
for j in range(number_of_inputs):
    l=tk.Label(my_w,text='Entry: '+str(j+1),font=20,fg='blue')
    l.grid(row=j,column=0,padx=3)

    e = tk.Entry(my_w, font=20,bg='lightyellow') 
    e.grid(row=j, column=1,padx=10,pady=3) 

    ref.append(e) # store references 
def my_check():
    my_flag=False
    for w in ref:
        if(len(w.get())<3):
            #print(w.get())
            my_flag=True
    if(my_flag==False):
        l1.config(text="Form can be submitted",fg='green')
    else:
        l1.config(text="Fill all the entries",fg='red' )
    l1.after(3000, lambda: l1.config(text=''))
b1=tk.Button(my_w,text='Submit',
 bg='lightgreen',command=lambda: my_check(),font=18)
b1.grid(row=j+1,column=0,padx=3,pady=5)
l1=tk.Label(my_w,text='Output here',font=20) # display message
l1.grid(row=j+1,column=1)
my_w.mainloop()  # Keep the window open
Entry Dynamic Buttons Dynamic Label Dynamically managing Spinboxes Label
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-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer