Python Forum

Full Version: Progress Bar While Sending an Email
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I am trying to create a progress bar for my tkinter based gui that shows the user that an email is being sent. I've never successfully implemented threading or multi-processing before, simply because any application I thought I would need either of them, it was easier and faster without. My thought was that I would need to create a popup tkinter object with a progress bar that polls a separate function that sends the email. These two function would then be combined in a single caller function that uses concurrent.futures. I'm not really sure, however, how I can poll the send_mail function from the mail_waitbox function.

Here's the relevant code from my project:

class MainApplication(ttk.Frame):
    def __init__(self, parent, *args, **kwargs):
        ttk.Frame.__init__(self, parent, *args, **kwargs)

    #gui setup and variables.

        self.email_test_button = ttk.Button(self.email_tab, width=36, text="Test Email Settings", command=self.check_email)
        self.email_test_button.grid(row=11, column=1, padx=20, pady=5)

    def check_email_waitbox(self):
        popup = tk.Toplevel()
        popup_label = ttk.Label(popup, text="Message being sent").grid(row=0, column=0)
        progress = 0
        progress_var = tk.DoubleVar()
        progress_bar = ttk.Progressbar(popup, variable=progress_var, maximum=100)
        progress_bar.grid(row=1, column=0)

    def check_email_backend(self):
        t1=time.perf_counter()
        print(t1)
        sender_name = self.display_email_from.get()
        test = "testlog.txt"
        for x in self.display_email_recipient:
            recipients = [x]
        with open(test, "w+") as f:
            f.write(f"From: {sender_name}\n")
            f.write(f"To: {recipients}\n")
            f.write("Subject: Testing\n\n")
            f.write("This is a test mailer.")
        try:
            server = smtplib.SMTP_SSL(self.display_email_smtp.get(), self.display_email_port.get())
            server.ehlo()
            server.login(self.display_email_login.get(), self.display_email_passwd.get())
            with open(test) as fp:
                msg = EmailMessage()
                msg.set_content(fp.read())
            msg["Subject"] = "Test"
            msg["From"] = self.display_email_from.get()
            msg["To"] = recipients
            server.send_message(msg)
            server.close()
        except Exception as ex:
            if "getaddrinfo" in str(ex):
                messagebox.showerror("Something Went Wrong", "Check your settings or your internet connection before continuing.")
            if "A connection attempt failed" in str(ex):
                messagebox.showerror("something Went Wrong", f"Check your settings.\n\nError Returned: \n{ex}")
            print(ex)
        t2=time.perf_counter()
        print(t1 - t2)

    def check_email(self):
        self.check_email_backend()
        # with concurrent.futures.ThreadPoolExecutor() as executor:
        #     g1 = executor.submit(self.check_email_waitbox)
        #     g2 = executor.submit(self.check_email_backend)

if __name__ == "__main__":
    root = tk.Tk()
    MainApplication(root)
    root.mainloop()
Note: I know what I have won't work. I'm just not really sure how to proceed from here.