Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I would like to clear the written email and write password grid when a del button is pressed, and then I can still save new emails and passwords, how do I do this? I tried to create a def clear using grid_slaves, but it erases everything, I want it to delete just write an email and write a password

from tkinter import *

roots = Tk()

roots.title("Email's save")

roots.geometry("500x500")

e = Entry(roots)

e.grid(row=0, column=1)

e.focus_set()

p = Entry(roots, show="*")

p.grid(row=1, column=1)

p.focus_set()

textEmail = StringVar()

textPassword = StringVar()

def callback():

    textEmail.set(textEmail.get() + e.get() + "\n")

    textPassword.set(textPassword.get() + p.get() + "\n")

def EmailPassword():

    email = Label(roots, text="Email: ",font=('Courier', 14))

    email.grid(row=0, sticky=W)

    passoword = Label(roots, text="Password: ",font=('Courier', 14))

    passoword.grid(row=1, sticky=W)

    saved_email = Label(roots, text="Saved Email",font=('Courier', 14))

    saved_email.grid(row=15, column=0)

    saved_password = Label(roots, text="Password",font=('Courier', 14))

    saved_password.grid(row=15, column=15)

    write_email = Label(roots, textvariable=textEmail,font=('Courier', 14))

    write_email.grid(row=20, column=0)

    write_password = Label(roots, textvariable=textPassword,font=('Courier', 14))

    write_password.grid(row=20, column=15)

    btn_save = Button(roots, text="Save", command= callback)

    btn_save.grid(row=10, column=2, sticky=W)

    btn_del = Button(roots, text="X", fg="red")

    btn_del.grid(row=60, column= 20)

    roots.mainloop()

EmailPassword()

1 Answer

0 votes
by (36.8k points)

The grid is just a single text field with newlines. To clear it, just set the text value to "".

Try this code.

from tkinter import *

roots = Tk()

roots.title("Email's save")

roots.geometry("500x500")

e = Entry(roots)

e.grid(row=0, column=1)

e.focus_set()

p = Entry(roots, show="*")

p.grid(row=1, column=1)

p.focus_set()

textEmail = StringVar()

textPassword = StringVar()

def callback():

    textEmail.set(textEmail.get() + e.get() + "\n")

    textPassword.set(textPassword.get() + p.get() + "\n")

def clearentries():

    e.delete(0, 'end')

    p.delete(0, 'end')

    

def cleargrid():

    textEmail.set("")

    textPassword.set("")

    

def EmailPassword():

    print ('save')

    global lstlabels

    email = Label(roots, text="Email: ",font=('Courier', 14))

    email.grid(row=0, sticky=W)

    passoword = Label(roots, text="Password: ",font=('Courier', 14))

    passoword.grid(row=1, sticky=W)

    saved_email = Label(roots, text="Saved Email",font=('Courier', 14))

    saved_email.grid(row=15, column=0)

    saved_password = Label(roots, text="Password",font=('Courier', 14))

    saved_password.grid(row=15, column=15)

    write_email = Label(roots, textvariable=textEmail,font=('Courier', 14))

    write_email.grid(row=20, column=0)

    write_password = Label(roots, textvariable=textPassword,font=('Courier', 14))

    write_password.grid(row=20, column=15)

    

    btn_save = Button(roots, text="Save", command= callback)

    btn_save.grid(row=10, column=2, sticky=W)

    btn_del = Button(roots, text="X", fg="red", command= cleargrid) # reset grid

    btn_del.grid(row=60, column= 20)

    roots.mainloop()

def key(event):  # listen for key press

    if event.keysym == 'Delete':

       clearentries() # clear text boxes

    

roots.bind_all('<Key>', key)

EmailPassword() 

If you are a beginner and want to know more about Python the do check out the python for data science course 

Related questions

0 votes
1 answer
0 votes
1 answer
asked Oct 3, 2019 in Python by Sammy (47.6k points)

Browse Categories

...