title: tkinter.Text: binding a variable to widget text contents
body: <p>Using Python 3.3 platform independent for this question.</p>
  
  <p>For the <code>Entry</code> widget, you can bind a variable to this widget's text contents like so (note the <code>textvariable</code> parameter in <code>Entry</code> constructor):</p>
  
  <pre><code>var = tkinter.StringVar()
  entryField = tkinter.Entry(master, textvariable=var)
  e.pack()
  
  var.set("a new value") # entryField text now updated with this value
  s = var.get() # whatever text now appears in entryField
  </code></pre>
  
  <p>For the <code>Text</code> widget however, there is no such variable binding feature.  Class <code>Text</code> definition should likely begin at line 2927 in %python dir%/Lib/tkinter/__init__.py for Python 3.3 in Windows releases if interested.</p>
  
  <p>How can I best emulate this variable binding feature with the <code>Text</code> widget?  My idea is to bind a <code>tkinter.StringVar</code> to a <code>Text</code> widget and just get/set all text.</p>
  
  <h1><strong>Update:</strong></h1>
  
  <p>I ended up inheriting <code>tkinter.Frame</code> as a <code>Text</code> wrapper which takes in a <code>textvariable</code> constructor parameter expected as a <code>tkinter.Variable</code> instance.  The only reason in my example below why I didn't inherit from <code>Text</code> is just because I wanted a scrollbar too, but that's not important.</p>
  
  <p>The following is my experimental code.  For exact relevance to my original question and how the problem was resolved (?), the important lines are <code>self.textvariable.get = self.GetText</code> and <code>self.textvariable.set = self.SetText</code>.  Basically, I'm overriding the passed-in <code>tkinter.Variable</code> object's get and set methods to my own devices...</p>
  
  <pre><code>class TextExtension( tkinter.Frame ):
      """Extends Frame.  Intended as a container for a Text field.  Better related data handling
      and has Y scrollbar now."""
  
  
      def __init__( self, master, textvariable = None, *args, **kwargs ):
          self.textvariable = textvariable
          if ( textvariable is not None ):
              if not ( isinstance( textvariable, tkinter.Variable ) ):
                  raise TypeError( "tkinter.Variable type expected, {} given.".format( type( textvariable ) ) )
              self.textvariable.get = self.GetText
              self.textvariable.set = self.SetText
  
          # build
          self.YScrollbar = None
          self.Text = None
  
          super().__init__( master )
  
          self.YScrollbar = tkinter.Scrollbar( self, orient = tkinter.VERTICAL )
  
          self.Text = tkinter.Text( self, yscrollcommand = self.YScrollbar.set, *args, **kwargs )
          self.YScrollbar.config( command = self.Text.yview )
          self.YScrollbar.pack( side = tkinter.RIGHT, fill = tkinter.Y )
  
          self.Text.pack( side = tkinter.LEFT, fill = tkinter.BOTH, expand = 1 )
  
  
      def Clear( self ):
          self.Text.delete( 1.0, tkinter.END )
  
  
      def GetText( self ):
          text = self.Text.get( 1.0, tkinter.END )
          if ( text is not None ):
              text = text.strip()
          if ( text == "" ):
              text = None
          return text
  
  
      def SetText( self, value ):
          self.Clear()
          if ( value is not None ):
              self.Text.insert( tkinter.END, value.strip() )
  </code></pre>
  
  <p>Side note: It's probably pretty obvious I'm coming from a different language based on spacing.  I'm sorry, I can't help it.</p>
  
  <p>I think I answered my own question.  Whether or not this is the right thing to do to override the known methods of <code>tkinter.Variable</code> objects passed into my functions like I just did is a separate question I'll have to ask/research even though this is a private bit of code that will never be used outside my app.  And I acknowledge that this does beg the question whether this is an effective solution at all.</p>