Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Reset button to remove all user input data

I’m having a problem with the "Clear Data" Button I created since I’m new to Tkinter and if anyone can help me clear the user data input once I click the button. Thank you in advance!
Thank you in advance!
Thank you in advance!
Thank you in advance!
Thank you in advance!
Thank you in advance!
Thank you in advance!

global entry
entry = StringVar()

def cleardata():
    entry.delete(1.0, END)

def btn1function():
    top = tkinter.Toplevel(root)
    top.title("ADD RECORDS")
    frame = tk.Frame(top)
    bg = ImageTk.PhotoImage(file="latestadrec.png")
    labl = tk.Label(frame, image=bg)
    labl.img = bg
    labl.place(relx=0.5, rely=0.5, anchor='center')

    fname = tk.Label(frame, text="First Name:", font=('Bold', 16))
    fname.place(x=50, y=400)
    fnameEntry = tk.Entry(frame, textvariable=firstname, font=('Bold', 16))
    fnameEntry.place(x=220, y=400, width=180)

    lname = tk.Label(frame, text="Last Name:", font=('Bold', 16))
    lname.place(x=50, y=450)
    lnameEntry = tk.Entry(frame, textvariable=lastname, font=('Bold', 16))
    lnameEntry.place(x=220, y=450, width=180)

    address = tk.Label(frame, text="Address:", font=('Bold', 16))
    address.place(x=50, y=500)
    addressEntry = tk.Entry(frame, textvariable=address1, font=('Bold', 16))
    addressEntry.place(x=220, y=500, width=180)

    conum = tk.Label(frame, text="Contact Number:", font=('Bold', 16))
    conum.place(x=50, y=550)
    conumEntry = tk.Entry(frame, textvariable=connum, font=('Bold', 16))
    conumEntry.place(x=220, y=550, width=180)

    email = tk.Label(frame, text="Email:", font=('Bold', 16))
    email.place(x=50, y=600)
    emailEntry = tk.Entry(frame, textvariable=email1, font=('Bold', 16))
    emailEntry.place(x=220, y=600, width=180)

    petname = tk.Label(frame, text="Pet name:", font=('Bold', 16))
    petname.place(x=650, y=400)
    petnameEntry = tk.Entry(frame,textvariable=pname, font=('Bold', 16))
    petnameEntry.place(x=790, y=400, width=180)

    gender = tk.Label(frame, text="Gender:", font=('Bold', 16))
    gender.place(x=650, y=450)
    genderEntry = tk.Entry(frame,textvariable=gender1, font=('Bold', 16))
    genderEntry.place(x=790, y=450, width=180)

    breed = tk.Label(frame, text="Breed:", font=('Bold', 16))
    breed.place(x=650, y=500)
    breedEntry = tk.Entry(frame,textvariable=breed1, font=('Bold', 16))
    breedEntry.place(x=790, y=500, width=180)

    birth = tk.Label(frame, text="Birth Date:", font=('Bold', 16))
    birth.place(x=650, y=550)
    birthEntry = tk.Entry(frame,textvariable=birth1, font=('Bold', 16))
    birthEntry.place(x=790, y=550, width=180)

    problem = tk.Label(frame, text="Problem:", font=('Bold', 16))
    problem.place(x=650, y=600)
    problemEntry = tk.Entry(frame,textvariable=problem1, font=('Bold', 16))
    problemEntry.place(x=790, y=600, width=180)

    button = tk.Button(frame, text="SUBMIT DATA", command=savefile, font=('Bold', 14))
    button.place(x=300, y=700)

    button2 = tk.Button(frame, text="CLEAR DATA", command=cleardata, font=('Bold', 14))
    button2.place(x=550, y=700)

    frame.pack(pady=10)
    frame.pack_propagate(False)
    frame.configure(width=1000, height=800)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You will have to use the delete method of the Entry widget if you are looking to delete text, better to put all the entries inside a list and loop through them rather doing it one by one.

def cleardata():
    for ent in ent_list:
        ent.delete(0, 'end')

def btn1function():
    global ent_list
    ...
    ent_list = [fnameEntry, lnameEntry, ...]

Or you could also create the list in the main part (outside of function) and then use its append() method to add each entry onto the list. The first solution might be less code and more readable.

Or if you want to avoid globalizing the list, then pass the list as an argument instead:

def cleardata(ent_list):
    for ent in ent_list:
        ent.delete(0, 'end')

def btn1function():
    ...
    ent_list = [fnameEntry, lnameEntry, ...]
    
    button2 = tk.Button(..., command=lambda: cleardata(ent_list))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading