title: How to continue code if user does not select a file in tkinter
body: <p>I'm new to tkinter and trying to create an email gui.  So far everything works but after adding a select file functionality to it the program will only work if the user selects a file.  I get the error </p>
  
  <pre><code>'GetInfo' object has no attribute 'fname'
  </code></pre>
  
  <p>Which I'm assuming means that since fname (file name) has no value added to it the program won't continue.  I tried to use a try/except thing here:</p>
  
  <pre><code>try:
          filename = self.fname
          attachment = open(self.fname, "rb")
  
          part = MIMEBase('application', 'octet-stream')
          part.set_payload((attachment).read())
          encoders.encode_base64(part)
          part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
  
          msg.attach(part)
  
  
      except:
          server = smtplib.SMTP('smtp.gmail.com', 587)
          server.starttls()
          server.ehlo()
          server.login(self.frominf.get(), self.passinf.get()) #sender email, password
          text = msg.as_string()
          server.sendmail(self.frominf.get(), self.toinf.get(), text)
          server.quit()
  
              # STOP TIMER
          elapsed_time = timer() - start # in seconds
          print ("Email took " + (str(elapsed_time)) + " seconds to send, sent to " + self.toinf.get() )
          time.sleep(0.5)
          root.destroy()
  </code></pre>
  
  <p>But this pretty much did the opposite, giving an error if I did supply a file.
  So my question is: how can I let the user decide if they want to supply a file or not?</p>
  
  <p>Here is my whole code:</p>
  
  <pre><code>from tkinter import *
  from tkinter.filedialog import askopenfilename
  from tkinter import ttk
  import time
  import datetime
  import smtplib
  from timeit import default_timer as timer
  from email.mime.multipart import MIMEMultipart
  from email.mime.text import MIMEText
  from email.mime.base import MIMEBase
  from email import encoders
  
  #colors
  darkred = (139, 0, 0)
  grey = (90, 90, 90)
  
  root = Tk()
  root.title("Email")
  
  
  
  class GetInfo:
  
      def __init__(self, master):
          #window size
          master.minsize(width=250, height=75)
  
          #senders email
          Label(root, text="Your email").pack(fill=X)
          self.frominf = Entry(master)
          self.frominf.pack(fill=X)
  
          #sending to email
          Label(root, text="Sending to").pack(fill=X)
          self.toinf = Entry(root)
          self.toinf.pack(fill=X)
  
          #subject line
          Label(root, text="Subject").pack(fill=X)
          self.subinf = Entry(root)
          self.subinf.pack(fill=X)
  
          #add file
          Label(root, text="File?").pack(fill=X)
          file_button = Button(root, text="Click to browse files", command= self.Load_file,  height = 1, bg="grey")
          file_button.pack(fill=X)
  
          #senders password
          Label(root, text="Your password").pack(fill=X)
          self.passinf = Entry(master, show="*")
          self.passinf.pack(fill=X)
          #body
          Label(root, text="Body of email").pack(fill=X)
          self.bodyinf = Text(root)
          self.bodyinf.pack(fill=X) 
  
          #submit button
          submit_button = Button(root, text="Submit(click when done)", command=self.send_email,  height = 2, bg="darkred")
          submit_button.pack(fill=X)
  
      def Load_file(self):
          self.fname = askopenfilename(filetypes=(("All Files","*.*"),
                                             ("HTML files", "*.html;*.htm"),
                                             ("Template files", "*.tplate") ))
  
  
      def send_email(self):
          # START TIMER
          start = timer()
          msg = MIMEMultipart()
  
          msg['From'] = self.frominf.get()
          msg['To'] = self.toinf.get()
          msg['Subject'] = self.subinf.get()
  
          body = self.bodyinf.get('1.0', END)
          msg.attach(MIMEText(body, 'plain'))
  
          filename = self.fname
          attachment = open(self.fname, "rb")
  
          part = MIMEBase('application', 'octet-stream')
          part.set_payload((attachment).read())
          encoders.encode_base64(part)
          part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
  
          msg.attach(part)
  
  
  
          server = smtplib.SMTP('smtp.gmail.com', 587)
          server.starttls()
          server.ehlo()
          server.login(self.frominf.get(), self.passinf.get()) #sender email, password
          text = msg.as_string()
          server.sendmail(self.frominf.get(), self.toinf.get(), text)
          server.quit()
  
              # STOP TIMER
          elapsed_time = timer() - start # in seconds
          print ("Email took " + (str(elapsed_time)) + " seconds to send, sent to " + self.toinf.get() )
          time.sleep(0.5)
          root.destroy()
  
  
  app = GetInfo(root)
  root.mainloop()
  </code></pre>