str1=tk.StringVar(master,value,name)
master
: (Optional)The variable is associated with, default value is parent window.value
:(Optional) We can set the initial value for the variable. name
: (Optional) Name given default is PY_VAR1 trace_add(self, mode, callback)
For an StringVar() we can check the different modes of this variable and trigger call back functions. This is the main advantage of using such variables.
db1.trace_add(['write','read'],my_r) # callback when data changes
b1 = tk.Button(my_w,text='Update',command=lambda:str1.set('Welcome') )
This triggers the trace_add() 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("www.plus2net.com") # Adding a title
def my_r(*args):
print(str1.get()) # Print when variable changes.
str1 = tk.StringVar(my_w) # declare StringVar()
str1.set('Hello')
l3 = tk.Label(my_w, textvariable=str1, width=15 )
l3.grid(row=2,column=1)
b1 = tk.Button(my_w,text='Update',command=lambda:str1.set('Welcome'))
b1.grid(row=2,column=3)
str1.trace_add('write',my_r)
my_w.mainloop()
Output
Welcome
str1.set('Hello') # assign value to str1
print(str1.get()) # display the value assigned to str1
str1=tk.StringVar(value='Option 1') # Assign value to str1
str1.set("")
len(str1.get())
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("400x150") # Size of the window width x height
my_w.title("plus2net.com") # Adding a title
e1_str=tk.StringVar() # declaring a StringVar()
e1 = tk.Entry(my_w,textvariable=e1_str,bg='yellow',font=28) # Entry box
e1.grid(row=0,column=0,padx=10,pady=30)
l1 = tk.Label(my_w, text='No of Chars here' ,font=28) # added one Label
l1.grid(row=0,column=1)
def my_upd(*args):
l1.config(text=str(len(e1_str.get()))) # read & assign text to StringVar()
e1_str.trace_add('write',my_upd) # triggers on change of StringVar
my_w.mainloop()
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("450x200") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
def my_r(*args):
print(str1.get()) # Print when variable changes
bt1.config(text=str1.get()) # Update button text
str1 = tk.StringVar(my_w) # Declare StringVar()
str1.set('Hello')
l3 = tk.Label(my_w, textvariable=str1, width=15, font=22, bg='yellow')
l3.grid(row=2, column=1, padx=2, pady=20)
bt1 = tk.Button(my_w, text='Update', command=lambda: str1.set('Welcome'))
bt1.grid(row=2, column=3, padx=10)
e1 = tk.Entry(my_w, textvariable=str1, bg='lightgreen', font=22)
e1.grid(row=2, column=4)
str1.trace_add('write', my_r) # Callback function on write mode
my_w.mainloop() # Keep the window open
import tkinter as tk
import random
my_w = tk.Tk()
my_w.geometry("300x200") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
str1 = tk.StringVar(value='Randome List') # Using StringVar
my_list = ['King', 'Queen', 'Jack',
'Ronald', 'Ram', 'Tina',
'Vik']
lb1 = tk.Label(my_w, textvariable=str1, width=15, bg='yellow', font=22)
lb1.grid(row=0, column=0, padx=30, pady=50)
def my_fun():
random_element = random.choice(my_list) # Random selection
str1.set(random_element) # Update StringVar
my_w.after(1000, my_fun) # Call the function after a delay
my_fun()
my_w.mainloop() # Keep the window open
import tkinter as tk
my_w = tk.Tk() # parent window
my_w.geometry("500x300") # width and height of window
font1 = ["Arial", 22, "normal"] # higher size font
my_str = tk.StringVar() # StringVar for Entry and Label
l1 = tk.Label(my_w, text="Name", font=font1) # added one Label
l1.grid(row=1, column=1)
t1 = tk.Entry(my_w, width=20, bg="yellow",
font=font1, textvariable=my_str) # added one text box
t1.grid(row=1, column=2, padx=10)
l2 = tk.Label(my_w, text="I will update", width=20, bg="lightgreen",
font=font1, textvariable=my_str, anchor="w") # added one Label
l2.grid(row=2, column=2, columnspan=3, padx=10, pady=20)
t1.focus() # Keep the cursor on Entry widget on opening
my_w.mainloop() # to keep the window open
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("300x100") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
def my_fun():
str2 = tk.StringVar(my_w,value='Welcome2') #
print(str2.get())
str2.trace_add('unset',my_unset) # variable is removed
str1 = tk.StringVar(my_w,value='Welcome1') #
def my_write(*args):
print(str1.get()) # Print when variable changes.
print('write is triggerd')
def my_read(*args):
print(str1.get()) # Print when variable accessed.
print('read is triggerd')
def my_unset(*args):
print('unset is triggerd')
bt1 = tk.Button(my_w,text='write',command=lambda:str1.set('plus2net'))
bt1.grid(row=1,column=1,padx=20,pady=10)
bt2 = tk.Button(my_w,text='read',command=lambda:str1.get())
bt2.grid(row=1,column=2,padx=20,pady=10)
bt3 = tk.Button(my_w,text='unset',command=my_fun)
bt3.grid(row=1,column=3,padx=20,pady=10)
str1.trace_add('write',my_write) # callback when data changes
str1.trace_add('read',my_read) # callback when data read
my_w.mainloop()
StringVar()
in Tkinter?StringVar()
object in Tkinter?StringVar()
is used to link a widget to a variable in Tkinter?StringVar()
object?StringVar()
object?StringVar()
object dynamically?StringVar()
to display the value of an Entry widget?StringVar()
object?StringVar()
object?StringVar()
object?22-03-2021 | |
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 |
23-03-2021 | |
Thanks, Let us use str1 |