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

Tkinter delete methode

I’m using Tkinter, I defined the function below to clear entries but it only works for the entries that have default values even when I input new values in these entries it can delete them but it does not clear the entry that doesn’t have default values, Where is the problem, Why entry4 doesn’t get clear?

def clear_text():
    entry1.delete(0, END)
    entry2.delete(0, END)
    entry3.delete(0, END)
    if entry4:
        entry4.delete(0, END)

entry1 = ttk.Entry(left_frame,textvariable=text1)
entry1.insert(0,'1.5')
entry1.grid(row=0, column=1, ipadx=10, padx=0, pady=10)

entry2 = ttk.Entry(left_frame,textvariable=text2)
entry2.insert(0,'2')
entry2.grid(row=1, column=1, ipadx=10, padx=0, pady=10)

entry3 = ttk.Entry(left_frame,textvariable=text3)
entry3.grid(row=2, column=1, ipadx=10, padx=0, pady=10)
entry3.insert(0,'100')

entry4 = ttk.Entry(left_frame, textvariable=text4).grid(row=3, column=1, ipadx=10, padx=0, pady=10)

delete_button = ttk.Button(left_frame, text = "Delete", command=clear_text)
delete_button.grid(row=9, column=0, ipadx=10, padx=0, pady=10)

This is what will happen after I click the Delete button
First image

enter image description here

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

>Solution :

This line –

entry4 = ttk.Entry(left_frame, textvariable=text4).grid(row=3, column=1, ipadx=10, padx=0, pady=10)

is the problem.

When you define a Tkinter object, it is better to separate the .pack or .grid so that the variable entry4 refers to the tkinter entry object rather than the .grid method which is a nonetype.

Try this –

entry4 = ttk.Entry(left_frame, textvariable=text4)
entry4.grid(row=3, column=1, ipadx=10, padx=0, pady=10)
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