Basically, I want the body of a Text widget to change when a StringVar does.
A variable defined using StringVar() holds a string data where we can set text value and can retrieve it. Also, we can pass this variable to textvariable parameter for a widget like Entry. The widget will automatically get updated with the new value whenever the value of the StringVar() variable changes.
We can use the Tkinter text widget to insert text, display information, and get the output from the text widget. To get the user input in a text widget, we've to use the get() method.
A normal variable can be used to set the value for any application whenever it is required. However, we can take the user input by creating an instance of the StringVar() object.
StringVar is a class that provides helper functions for directly creating and accessing such variables in that interpreter. As such, it requires that the interpreter exists before you can create an instance. This interpreter is created when you create an instance of Tk .
Short version is, you can't. At least, not without doing extra work. The text widget doesn't directly support a variable
option.
If you want to do all the work yourself it's possible to set up a trace on a variable so that it keeps the text widget up to date, and you can add bindings to the text widget to keep the variable up to date, but there's nothing built directly into Tkinter to do that automatically.
The main reason this isn't directly supported is that the text widget can have more than just ascii text -- it can have different fonts and colors, embedded widgets and images, and tags.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With