Reputation: 373
import tkinter as tk
a = "hi"
print(a)
a1 = tk.StringVar()
a1.set("Hi")
print(a1)
hi ##(Output from first print function)
AttributeError: 'NoneType' object has no attribute '_root' (Output from second print function)
What is the difference between a
and a1
in above code and their use-cases. Why a1
is giving error?
Upvotes: 12
Views: 57893
Reputation: 41
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.
Upvotes: 0
Reputation: 370
At the beginning add
root = tk.Tk()
These Variables are designed for tkinter. and these do not work independently.
Upvotes: 1
Reputation: 2731
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!
Upvotes: 21
Reputation: 739
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
Upvotes: 11
Reputation: 386010
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.
Upvotes: 5