Python tkinter StringVar() trace

For an StringVar() so we can check the different modes of this variable, like this

r :Read - the variable is read by someone
w :Write- the variable is written by someone.
u :undefined – The variable is deleted

Here is an example which uses w ( write ) mode to display the value of the variable when ever it changes.
We used one Button and used on Click event to change the value of this variable str1 from Hello to Welcome.
b1 = tk.Button(my_w,text='Update',command=lambda:str1.set('Welcome') )
This triggers the trace() which used the callback function my_r() to print the value of the variable ( Welcome ) to our console.
import tkinter as tk
from tkinter import *
my_w = tk.Tk()
my_w.geometry("300x200")  # Size of the window 
my_w.title("")  # Adding a title

def my_r(*args):
    print(str1.get()) #  Print when variable changes.
str1 = tk.StringVar(my_w) # declare StringVar()

l3 = tk.Label(my_w,  textvariable=str1, width=15 )  

b1 = tk.Button(my_w,text='Update',command=lambda:str1.set('Welcome'))  



set() and get() methods of StringVar

In above examples we used set() method to assign data to the string variable and to read the data stored we used get() method. These two methods are frequently used in our scripts.
str1.set('Hello') # assign value to str1
print(str1.get()) # display the value assigned to str1

Initializing StringVar

We can using set() method to assign data to StringVar after declaring or we can assign value while declaring the string variable.
str1=tk.StringVar(value='Option 1') # Assign value to str1

Length of StringVar

We can use len

Normal Variable and StringVar

StringVar() is a class in Tkinter. In a GUI application we require various events to be captured to trigger different functions (requirements). If we use normal Python variable with widgets then monitoring the changes is difficult. However by using a StringVar we can monitor the changes and trigger functions based on the requirements.

Example : Declare the password is strong or weak based on the number of chars entered by user. Here we can use StringVar and each time the variable changes its stored data, we can trigger the function checking the length of the input and show the message.
DoubleVar() IntVar() BooleanVar()
Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net


    Very bad to call a variable in Python "str"
    str = tk.StringVar(my_w) # declare StringVar()
    because str is build-in method to stringify data!
    class str(object='') --> built-in function in Python


    Thanks, Let us use str1

    Post your comments , suggestion , error , requirements etc here

    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2022 All rights reserved worldwide Privacy Policy Disclaimer