12

Code:

import tkinter as tk
a = "hi"
print(a)
a1 = tk.StringVar()
a1.set("Hi")
print(a1)

Output:

hi ##(Output from first print function) 

AttributeError: 'NoneType' object has no attribute '_root' (Output from second print function) 

My question:

What is the difference between a and a1 in above code and their use-cases. Why a1 is giving error?

5 Answers 5

21

A StringVar() is used to edit a widget's text

For example:

import tkinter as tk


root = tk.Tk()
my_string_var = tk.StringVar()
my_string_var.set('First Time')
tk.Label(root, textvariable=my_string_var).grid()
root.mainloop()

Will have an output with a label saying First Time NOTE:textvariable has to be used when using string variables

And this code:

import tkinter as tk

def change():
    my_string_var.set('Second Time')

root = tk.Tk()
my_string_var = tk.StringVar()
my_string_var.set('First Time')
tk.Label(root, textvariable=my_string_var).grid()
tk.Button(root, text='Change', command=change).grid(row=1)
root.mainloop()

Produces a label saying First Time and a button to very easily change it to Second Time.

A normal variable can't do this, only tkinter's StringVar()

Hopes this answers your questions!

1
  • I like the answer!. I get it between the difference of directly changing the text of a Label widget vs doing it with StringVar , Thanks! this works also with others right? Like IntVar? and the others?
    – Ice Bear
    Commented Dec 18, 2020 at 4:15
11

StringVar() is a class from tkinter. It's used so that you can easily monitor changes to tkinter variables if they occur through the example code provided:

def callback(*args):
    print "variable changed!"

var = StringVar()
var.trace("w", callback)
var.set("hello")

This code will check if var has been over-written (this mode is defined by the w in var.trace("w", callback).

A string such as "hello" is just a data type, it can be manipulated and read and all sorts, the primary difference is that if the string was assigned to a variable, such as a = 'hello', there is no way of telling if a has changed (i.e if now a = 'hello') unless you do a comparison somewhere which could be messy.

Put it simply: StringVar() allows you to easily track tkinter variables and see if they have been read, overwritten, or if they even exist which you can't easily do with just a typical a = 'hello'

Helpful : http://effbot.org/tkinterbook/variable.htm

Edit : Replaced 'variables' with 'tkinter variables' where appropriate as per @Bryan Oakley's suggestion

1
  • 1
    I think your answer would be better if instead of "variables" you used something like "tkinter variables". You can't use a StringVar to monitor changes in ordinary variables, but your answer makes it sound like you can. Otherwise, it's a good answer. Commented Aug 10, 2018 at 12:49
5

Tkinter is a wrapper around an embedded tcl interpreter. 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. If you try to create an instance of StringVar before you initialize tkinter, you will get the error that is shown in the question.

Once tkinter has been properly initialized and a StringVar instance has been created, it can be treated like any other python object. It has methods to get and set the value that it represents.

1
  • I am new to Python. So, it would be great if you could explain your points with an example. This way I can understand quickly.
    – Msquare
    Commented Aug 13, 2018 at 6:00
1

At the beginning add

root = tk.Tk()

These Variables are designed for tkinter. and these do not work independently.

1
  • 1
    I think, the OP meant StringVar() and not normal variable and Tk() instance Commented Sep 16, 2020 at 16:11
0

Suppose if you are building a GUI calculator, you want to display the values the user inputs in the screen of the calculator. If the user is trying to add 5 + 5, we have to show, "5" "+" "5" in the display. And when the equals button is pressed, we want to display "10". That is the use of StringVar(). It holds the string equivalent of the value the interpreter holds.

Not the answer you're looking for? Browse other questions tagged or ask your own question.