How to clear entries in tkinter after clicking submit button?

gm1312

I am using Tkinter to make a user input form and take values from users and storing the values in a database when clicked on the submit button. I want to clear all the fields from the entry widgets after clicking the submit button so that the values get stored in the database as well as the entry widgets clear out so that the user can again fill the details. I tried the delete(0;'end') command but it did not work. Following is my code:

def database():
    customer_name = name.get()
    mobile_no = num.get()
    email_id = mail_id.get()
    address = addr.get()

    result = {
            "Customer Name":customer_name,
            "Mobile":mobile_no,
            "Email ID":email_id,
            "Address":address
    }

    print(result)
    db.Customer_Info.insert_one(result)
    print('Value Inserted')
    client.close()


##Creating tkinter window
def main_page():
    
    global window
    window = Tk()
    window.title('Products')
    window.geometry('400x500')

    global name,uname,mob,num,mail_id,addr
    name = StringVar()
    num = IntVar()
    num.set('')
    mail_id = StringVar()
    addr = StringVar()

    Label(window,text = 'HN Organics',background = 'green',
        foreground = 'white',font = ('Arial',20)).grid(column = 0,
        row = 0, padx = 120, pady =25)

    Label(window ,text = "Name").grid()
    uname = Entry(window,textvariable = name).grid()

    Label(window ,text = "Mobile No.").grid()
    mob = Entry(window,textvariable = num).grid()

    Label(window ,text = "Email ID").grid()
    mail = Entry(window,textvariable = mail_id).grid()

    Label(window ,text = "Address").grid()
    addr1 = Entry(window,textvariable = addr).grid()

    Button(window,text = 'Submit',command = database).grid()

    window.mainloop()
main_page()

When the submit button is clicked the values are stored in the database but I want the entries to clear as well after being stored in the database. How do I do that?? I tried using name.delete(0.'end') / name.delete(0.'END') and even name.delete(0.'Tkinter.END') but it shows that there is no attribute delete.

Gavya Mehta

You can add the set('') command in the database function after you store the values and it will clear out the entries so that the user can fill details again. Just add:

name.set('')
num.set('')
mail_id.set('')
addr.set('')

after client.close()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to clear input after clicking in button submit in Dash?

After clicking the submit button stop fields from getting clear

How to clear screen after clicking button?

How to clear canvas and buttons (and their functuality) after clicking a button?

how to post comment below the text box after clicking submit button?

How to display form data in popup after clicking on submit button

How to move selenium webdriver to next page after clicking submit button?

How can I move to the next page after clicking onto the submit button, I can't move even after clicking on the submit button

How to show "Loading..." and then run script after clicking button in tkinter?

How do I update a label after clicking a Button in Tkinter?

How to submit a form by clicking submit button only

nothing happens after clicking submit button

Staying on same page after clicking submit button

Get URL after clicking on Submit button of a form

after clicking submit button a white page appears

Change the url after clicking the submit button

Showing hidden table after clicking submit button

How to clear the tkinter label in loop and how to terminate the loop upon clicking another button

How clear form after submit

Restart the Combobox tkinter after clicking a button

Unable to clear input field automatically after clicking on submit (React)

How to disable card-view in a file after clicking submit button in another file

How to get value from XAML after clicking a submit button in Xamarin Forms?

how to get a control on a particular form after clicking submit button when i have multiple forms in a single page

Laravel form not working after clicking on the submit button. How can I fix this?

What causes a Streamlit form to refresh after clicking the submit button and how can I fix it?

How do I make my page stop refreshing after clicking submit button?

How to change, using React, the value of a form input after clicking the submit button

How to store variable value after clicking "Submit"?