Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I connect a StringVar to a Text widget in Python/Tkinter?

Tags:

python

tkinter

Basically, I want the body of a Text widget to change when a StringVar does.

like image 707
John Zhang Avatar asked May 15 '12 01:05

John Zhang


People also ask

How do I use StringVar in Python?

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.

How do I print and have user input in a text box in Tkinter?

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.

What is the difference between a variable and StringVar () in Tkinter?

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.

What is StringVar ()?

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 .


1 Answers

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.

like image 52
Bryan Oakley Avatar answered Nov 14 '22 22:11

Bryan Oakley