1

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)
  • 2
    You need to provide a [mre]. – acw1668 Nov 15 '22 at 14:20
  • I'm trying to place my code but it's rejecting me from placing the code saying my code is not formatted properly – Shukuchi Hikaru Nov 15 '22 at 14:22
  • Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon. I don't know what seems to be the problem – Shukuchi Hikaru Nov 15 '22 at 14:23
  • I'm going to say the same thing I said yesterday: You can easily format a block of code in your question by inserting three (3) backtick characters `, followed by your code, then another three backticks on a new line. You can also just paste your code into your question, highlight the entire block, and press ctrl+k on your keyboard to format the whole block as code. See [here](https://stackoverflow.com/editing-help) for help on formatting on StackOverflow – JRiggles Nov 15 '22 at 14:24
  • 1
    I'm really sorry about this mess and thank you as well always sir @Jriggles – Shukuchi Hikaru Nov 15 '22 at 14:29
  • What have you tried so far in your `cleardata` function? I'm afraid you're not likely to get much help if you're looking to have someone write code for you. – JRiggles Nov 15 '22 at 14:31
  • I tried to use global function that I can call inside the def cleardata function. Tried to use this entry.delete(1.0, END) but got an error AttributeError: 'StringVar' object has no attribute 'delete' – Shukuchi Hikaru Nov 15 '22 at 14:36

1 Answers1

2

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))
Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46