title: Tkinter Text Widget Save to File
body: <p>So, I'm quite new to using python and I wanted to learn more about it so I decided to download a sticky note program from the internet (<a href="https://rhinote.tuxfamily.org/" rel="nofollow noreferrer">Rhinote</a>) and study / modify it to use a menu bar instead of control commands to execute the methods. I also modified it to work with Python 3.x instead of Python 2.</p>
  
  <p>The problem I'm coming up with after modifying it with the menu bar is that it creates the file but doesn't actually write to it. I've been trying to fix this for a while now so any help is appreciated.</p>
  
  <p>Here is the code I have currently:</p>
  
  <pre><code>from tkinter import *
  import tkinter.filedialog, tkinter.messagebox
  import os
  from os import system
  
  # the root window:
  def Sticky():
      r = Tk()
      r.option_add('*font', '{Helvetica} 11')
      t = Text(r, bg = '#f9f3a9', wrap = 'word', undo = True)
      t.focus_set()
      t.pack(fill = 'both', expand = 1)
      r.geometry('220x235')
      r.title('Note')
  
      text = TextWidget()
  
      m = tkinter.Menu(r)
      m.add_command(label="+", command=text.new_window)
      m.add_command(label="Save", command=text.save_file)
      m.add_command(label="Save As", command=text.save_file_as)
      m.add_command(label="Open", command=text.open_file)
      r.config(menu=m)
  
      r.mainloop()
  
  # the text widget, and all of its functions:
  class TextWidget(Text):
      def __init__(self):
          Text.__init__(self)
          self.filename = ''
          self._filetypes = [
          ('Text', '*.txt'),
              ('All files', '*'),
              ]
  
      def save_file(self, whatever = None):
          if (self.filename == ''):
              self.save_file_as()
          else:
              f = open(self.filename, 'w')
              f.write(self.get('1.0', 'end'))
              f.close()
              tkinter.messagebox.showinfo('FYI', 'File Saved.')
  
      def save_file_as(self, whatever = None):
          self.filename = tkinter.filedialog.asksaveasfilename(defaultextension='.txt',
                                                               filetypes = self._filetypes)
          f = open(self.filename, 'w')
          f.write(self.get('1.0', 'end'))
          f.close()
          tkinter.messagebox.showinfo('FYI', 'File Saved')
  
      def open_file(self, whatever = None, filename = None):
          if not filename:
              self.filename = tkinter.filedialog.askopenfilename(filetypes = self._filetypes)
          else:
              self.filename = filename
          if not (self.filename == ''):
              f = open(self.filename, 'r')
              f2 = f.read()
              self.delete('1.0', 'end')
              self.insert('1.0', f2)
              f.close()
              self.title('Sticky %s)' % self.filename)
  
      def new_window(self):
          Sticky()
  
      def help(whatever = None):
          tkinter.messagebox.showinfo('Help', message = '''
  Help
  ''')
  # make it so:
  if __name__ == '__main__':
      Sticky()
  </code></pre>
  
  <p>And here is the Rhinote program I'm trying to modify:</p>
  
  <pre><code>from tkinter import *
  import tkinter.filedialog, tkinter.messagebox
  import os
  from os import system
  
  # the root window:
  def Rhinote():
      r = Tk()
      r.option_add('*font', '{Helvetica} 11')
      t = TextWidget(r, bg = '#f9f3a9', wrap = 'word', undo = True)
      t.focus_set()
      t.pack(fill = 'both', expand = 1)
      r.geometry('220x235')
      r.title('Rhinote')
      r.mainloop()
  
  # the text widget, and all of its functions:
  class TextWidget(Text):
  
      def save_file(self, whatever = None):
          if (self.filename == ''):
              self.save_file_as()
              self.master.title('Rhinote %s' % self.filename)
          else:
              f = open(self.filename, 'w')
              f.write(self.get('1.0', 'end'))
              f.close()
              self.master.title('Rhinote %s' % self.filename)
              tkinter.messageb ox.showinfo('FYI', 'File Saved.')
  
      def save_file_as(self, whatever = None):
          self.filename = tkinter.filedialog.asksaveasfilename(filetypes = self._filetypes)
          f = open(self.filename, 'w')
          f.write(self.get('1.0', 'end'))
          f.close()
          tkinter.messagebox.showinfo('FYI', 'File Saved')
  
      def open_file(self, whatever = None, filename = None):
          if not filename:
              self.filename = tkinter.filedialog.askopenfilename(filetypes = self._filetypes)
              self.master.title('Rhinote %s' % self.filename)
          else:
              self.filename = filename
              self.master.title('Rhinote %s' % self.filename)
          if not (self.filename == ''):
              f = open(self.filename, 'r')
              f2 = f.read()
              self.delete('1.0', 'end')
              self.insert('1.0', f2)
              f.close()
              self.master.title('Rhinote %s)' % self.filename)
  
      def new_window(self, event):
          Rhinote()
  
      def printfile(self, whatever = None):
          f = open(self.printfilename, 'w')
          f.write(self.get('1.0', 'end'))
          f.close
          # 'enscript' formats the text; lpr sends it to the default printer;
          # enscript's -B option suppresses page headers.
          system('enscript -B --word-wrap $HOME/.Rhinoteprintfile &gt; lpr &amp;')
  
      def help(self, whatever = None):
          tkinter.messagebox.showinfo('Rhinote Help', message = '''
  Editing Commands
      Ctrl-x : Cut selected text
      Ctrl-c : Copy selected text
      Ctrl-v : Paste cut/copied text
      Ctrl-Z : Undo
      Ctrl-Shift-z : Redo
  
  File Commands
      Ctrl-o : Open file
      Ctrl-s : Save current note
      Ctrl-a : Save current note as &lt;filename&gt;
      Ctrl-p : Print current note
      Ctrl-n : Open new Rhinote
  
  General
      Ctrl-h : Display this help window
  
  Rhinote version 0.7.4
  Free Software distributed under the GNU General Public License
  http://rhinote.tuxfamily.org
  ''')
  
      def __init__(self, master, **kw):
          Text.__init__(self, master, **kw)
          self.bind('&lt;Control-n&gt;', self.new_window)
          self.bind('&lt;Control-N&gt;', self.new_window)
          self.bind('&lt;Control-o&gt;', self.open_file)
          self.bind('&lt;Control-O&gt;', self.open_file)
          self.bind('&lt;Control-s&gt;', self.save_file)
          self.bind('&lt;Control-S&gt;', self.save_file)
          self.bind('&lt;Control-a&gt;', self.save_file_as)
          self.bind('&lt;Control-A&gt;', self.save_file_as)
          self.bind('&lt;Control-p&gt;', self.printfile)
          self.bind('&lt;Control-P&gt;', self.printfile)
          self.bind('&lt;Control-h&gt;', self.help)
          self.bind('&lt;Control-H&gt;', self.help)
          self.master = master
          self.filename = ''
          self.printfilename = os.environ['HOME']+'/.Rhinoteprintfile'
          self._filetypes = [
          ('Text/ASCII', '*.txt'),
          ('Rhinote files', '*.rhi'),
              ('All files', '*'),
              ]
  
  # make it so:
  if __name__ == '__main__':
      Rhinote()
  </code></pre>