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

StringVar()
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 str from Hello to Welcome.
b1 = tk.Button(my_w,text='Update'command=lambda:str.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
my_w = tk.Tk()
my_w.geometry("300x200")  # Size of the window 
my_w.title("www.plus2net.com")  # Adding a title

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

l3 = tk.Label(my_w,  textvariable=str, width=15 )  
l3.grid(row=2,column=1)

b1 = tk.Button(my_w,text='Update',command=lambda:str.set('Welcome'))  
b1.grid(row=2,column=3)

str.trace('w',my_r)

my_w.mainloop() 
Output
Welcome


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer