1

I want to replace the widgets in the tkinter gui, but when I am using .destroy, I am getting the object nonetype has no attribute destroy, I am not able to use a for loop to use .win_children and then for child in children.

This is the code:

import tkinter as tk
import smtplib

win = tk.Tk()
win.title("Email Automater")
win.geometry('400x200')
                         
def sendEmail(to, content):
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.login('parvanshusharma12@gmail.com', '*********')
    server.sendmail('parvanshusharma12@gmail.com', to, content)
    server.close()

def mailGui():
    tk.Label(win, text="Message:").place(x=20, y=20)
    msg = tk.Entry(win, width=250).place(x=230, y=50)

tk.Label(win, text="To:").place(x=20, y=20)
to_mail = tk.Entry(win, width=25).place(x=20, y=50)

tk.Label(win, text="From:").place(x=230, y=20)
from_mail = tk.Entry(win, width=25).place(x=230, y=50)

tk.Label(win, text="From Password:").place(x=230, y=80)
from_mail = tk.Entry(win, show='*', width=25).place(x=230, y=110)

tk.Button(win, text="Done!", command=mailGui).place(x=230, y=150)

win.mainloop()
a121
  • 798
  • 4
  • 9
  • 20

1 Answers1

0

If you want to destroy all the items you can use win.winfo_children() to get all the children and you can destroy them one by one. Something like this:

 for item in win.winfo_children():
        item.destroy()

But I suggest you place all the widgets in a frame and destroy the frame.

import tkinter as tk
import smtplib

win = tk.Tk()
win.title("Email Automater")
win.geometry('400x200')
                         
def sendEmail(to, content):
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.login('parvanshusharma12@gmail.com', '*********')
    server.sendmail('parvanshusharma12@gmail.com', to, content)
    server.close()

def mailGui():
    frame.destroy()
    
    tk.Label(win, text="Message:").place(x=20, y=20)
    msg = tk.Entry(win, width=250).place(x=230, y=50)

frame = tk.Frame(win)
frame.pack(expand=True, fill='both')

tk.Label(frame, text="To:").place(x=20, y=20)
to_mail = tk.Entry(frame, width=25).place(x=20, y=50)

tk.Label(frame, text="From:").place(x=230, y=20)
from_mail = tk.Entry(frame, width=25).place(x=230, y=50)

tk.Label(frame, text="From Password:").place(x=230, y=80)
from_mail = tk.Entry(frame, show='*', width=25).place(x=230, y=110)

tk.Button(frame, text="Done!", command=mailGui).place(x=230, y=150)

win.mainloop()

JacksonPro
  • 3,135
  • 2
  • 6
  • 29