title: Open FIle Python tkinter - problem with message box when introduced new input
body: <h2>Self Introduction</h2>
  <p>Hi, Im making a basic notepad with tkinter and I have 0 to no knoledge when it comes to coding, I'm not a programmer but I do enjoy seeing some code.</p>
  <h2>Current Project state</h2>
  <p>At the moment I have coded basic structure for, new_file, open_file, save_file, save_as_file and exit function, I have 2 main bugs both of them encountered in open_file().</p>
  <h2>Introduction to problem</h2>
  <p>I have a global url that stores the names of the files and one save flag that happens when i save the file. &quot;untitled&quot; to &quot;open_file&quot; works like intended, whoever, the main issue occurs when I type something new to the file.</p>
  <h2>Current Goal</h2>
  <p>My goal here is to create a simple functional notepad ---&gt; when I open_file and open another one without typing anything it wont ask me if I want to save file, If I open one file, type and proceed to open another file with will send me a pop up message asking me if I want to save changes, same to when I exit file.</p>
  <h2>My Bugs</h2>
  <p>One of the bugs happens when I click &quot;save_file&quot;, at the moment, everytime I click &quot;open file&quot; it will ask me if I want to save file no mather what I do, but as soon as I click save in one of the documents and open another file it won't ask me if I want to save anymore.</p>
  <p>It happens because of this line of code</p>
  <p>elif save_flag:
  open_file_function()</p>
  <p>Second &quot;bug&quot; is that there is no way to check if new input was added to the document so it can ask me if I want to save document</p>
  <h2>Global Variable</h2>
  <pre><code> #variable
   url = ''
   save_flag = False
  </code></pre>
  <h2>Open_file</h2>
  <pre><code>def open_file(e=None):
      text = text_editor.get('1.0', END)
      # If Nothing is written then directly exit.
      if len(text) == 1:
          open_file_function()
      elif save_flag:
          open_file_function()
      else:
          choice = askyesnocancel('Notepad', 'Do you want to save your changes to ' + main_application.title()
                                  + '?')
          if choice:
              save_file()
              open_file_function()
          elif choice is None:
              pass
          else:
              open_file_function()
  
  
  def open_file_function():
      global url
  
      url = filedialog.askopenfilename(initialdir=&quot;\Desktop&quot;, title='Select File',
                                       filetypes=(
                                       ('Text File', '*.txt'), (&quot;Python Files&quot;, &quot;*.py&quot;), (&quot;HTML Files&quot;, &quot;*.html&quot;),
                                       ('All files', '*.*')))
      try:
          with open(url, 'r') as fr:
              text_editor.delete(1.0, tk.END)
              text_editor.insert(1.0, fr.read())
      except FileNotFoundError:
          return
      except:
          return
      main_application.title(os.path.basename(url))
  
      name = url
      name = pathlib.Path(name).name
      main_application.title(f'{name} - NotePad')
  </code></pre>
  <h2>Save_file and Save_as_file</h2>
  <pre><code>def save_file(e=None):
      global url
      global save_flag
  
      if url:
          # Save the file
          text_file = open(url, 'w')
          text_file.write(text_editor.get(1.0, END))
          # Close the file
          text_file.close()
          # Put status update or popup code
          name = url
          name = pathlib.Path(name).name
          main_application.title(f'{name} - NotePad')
          save_flag = True
      else:
          save_as_file()
  
  
  def save_as_file(e=None):
      global url
  
      url = filedialog.asksaveasfilename(initialdir=&quot;\Desktop&quot;, title=&quot;Save As&quot;, defaultextension=&quot;.txt&quot;, filetypes=(
          (&quot;Text Files&quot;, &quot;*.txt&quot;), (&quot;Python Files&quot;, &quot;*.py&quot;), (&quot;HTML Files&quot;, &quot;*.html&quot;)))
      if url:
          # Update Status Bars
          name = url
          name = pathlib.Path(name).name
          main_application.title(f'{name} - NotePad')
          take_file = open(url, &quot;w&quot;)
          take_text = text_editor.get(1.0, END)
          take_file.write(take_text)
          take_file.close()
          messagebox.showinfo(&quot;Successfully Saved&quot;, &quot;Your file saved successfully&quot;)
  </code></pre>
  <h2>Exit_funtion</h2>
  <pre><code>def exit_application():
      text = text_editor.get('1.0', END)
      # If Nothing is written then directly exit.
      if len(text) == 1:
          main_application.destroy()
      # Else check whether it has saved if yes then exit else prompt to save it.
      elif save_flag:
          main_application.destroy()
  
      else:
          choice = askyesnocancel('Notepad', 'Do you want to save your changes to ' + main_application.title()
                                  + '?')
          if choice:
              save_file()
              main_application.destroy()
          elif choice is None:
              pass
          else:
              main_application.destroy()
  </code></pre>