Home > database >  Deleting entry boxes
Deleting entry boxes

Time:11-05

I am trying to build up a program to collect data from users. For this aim, the user set the amount of entry boxes and the entry boxes are created. My problem is related to when the user set 4 entry boxes are created 4 entry boxes, after that, the user could set 2 entry boxes but it is still shown the last 4 entry boxes, I mean, I need to delete all the entry boxes created by the user before and to create the new ones.I tried winfo_children() but it eliminates all the entry boxes even the one to set the amount of entry boxes by user. Would you help me, please? Thank you in advance,

from tkinter import *

# Creating main screen
root  = Tk() 

# Function to set amount of entry boxes
def boxes():

    # Saving amount of boxes
    amount = int(number_boxes.get())

    for i in range(amount):
        for y in range(4):
            set_boxes = Entry(root, borderwidth=3)
            set_boxes.grid(row=i 4, column=y)
     
# Creating label for number of boxes
label_number_boxes = Label(root, text='Enter the number of boxes')
# Showing in screen
label_number_boxes.grid(row=0, column=0)

# Creating number of boxes
number_boxes = Entry(root, width=20, bg='white', fg='black', borderwidth=3)
# Showing in screen
number_boxes.grid(row=1, column=0)

# Creating button to set the amount of boxes
button_number_boxes = Button(root, text='Set amount of boxes', command=boxes)
# Showing in screen
button_number_boxes.grid(row=2, column=0)

# Creating infinite loop to show in screen
root.mainloop()

CodePudding user response:

Use a list to store those Entry widgets, then you can use this list to destroy the Entry widgets before creating new ones:

entries = []

# Function to set amount of entry boxes
def boxes():
    # clear old entry boxes
    for w in entries:
        w.destroy()
    entries.clear()

    # Saving amount of boxes
    amount = int(number_boxes.get())

    for i in range(amount):
        for y in range(4):
            set_boxes = Entry(root, borderwidth=3)
            set_boxes.grid(row=i 4, column=y)
            entries.append(set_boxes) # save the Entry widget
  • Related