Basically, the StringVar() method is used for editing a widget's text
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()
Here, We will be having an output with a label saying First time
Note:-
We have to use textvariable when we are using string variables
Check 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()
Here you see, This code produces a label saying First Time and also a button to change it to Second Time
See, with the help of a normal variable, we can't achieve this, only tkinter's StringVar() can do this.
Interested to learn more about Python? Come and join Python Online course