Abhimanyu Sharma
Abhimanyu Sharma

Reputation: 903

Difference Between Variable And Textvariable in tkinter

My code:

my_var = tk.StringVar()

m_var2 = tk.StringVar()

my_entry1 = ttk.Entry(root, width=16, textvariable=my_var)

my_radio1 = ttk.Radiobutton(root, text="1", variable=my_var2)

Now what is the difference between textvariable and variable ?

Upvotes: 2

Views: 7050

Answers (2)

OpOp
OpOp

Reputation: 1

In Tkinter, a variable and a textvariable are both types of variable data. A textvariable is a variable that is displayed as text. When the variable changes, the text of the widget changes as well.

A text variable is a type of variable that represents an unknown text value. For example, if you want to ask a template user for their first name, you would create a Text variable in your template to capture that specific item of data.

A variable defined using StringVar() holds a string data where you can set text value and can retrieve it. You 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.

Upvotes: 0

Bryan Oakley
Bryan Oakley

Reputation: 386010

variable and textvariable are similar in concept, in that both represent variable data.

In the case of textvariable, which is mostly used with Entry and Label widgets, it is a variable that will be displayed as text. When the variable changes, the text of the widget changes as well.

In the case of variable, which is mostly used for Checkbutton and Radiobutton widgets, it represents the value that the widget holds. For example, you may have radio buttons with the text "Yes" and "No", but the actual values may be 1 and 0. In this case the variable represents the value of the widget.

Upvotes: 1

Related Questions