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.
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.
Comments