Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Setting and Retrieving values of Tkinter variable - Python
Next article icon

There is no inbuilt way to track variables in Python. But tkinter supports creating variable wrappers that can be used to do so by attaching an 'observer' callback to the variable. The tkinter.Variable class has constructors like BooleanVar, DoubleVar, IntVarand StringVar for boolean, double-precision floating-point values, integer and strings respectively, all of which can be registered to an observer that gets triggered every time the value of the variable is accessed. The observer remains active until it is explicitly deleted. It is also important to note that the callback function associated with the observer takes three arguments by default i.e. Name of the Tkinter variable, index of the tkinter variable in case it's an array otherwise an empty string and the mode of access.
In Python 3.5 and 2.7 above older methods such as trace_variable(), trace(), trace_vdelete () and trace_vinfo() are replaced by the below mentioned methods.
 

  1. trace_add (): 
    The trace_add () method has replaced trace_variable() method. The trace_add() method is used to add an observer to a variable and returns the name of the callback function whenever the value is accessed.
     

Syntax : trace_add(self, mode, callback_name) 
Parameters: 
Mode: It is one of "array", "read", "write", "unset", or a list or tuple of such strings. 
callback_name: It is the name of the callback function to be registered on the tkinter variable. 
 


  1.  
  2. trace_remove():
    The trace_remove() method replaces trace_vdelete() method and it is used to unregister an observer. It returns the name of the callback used while registering the observer through trace_add() method initially. 
     

Syntax : trace_remove(self, mode, callback_name) 
Parameters: 
Mode: It is one of "array", "read", "write", "unset", or a list or tuple of such strings. 
callback_name: It is the name of the callback function to be registered on the tkinter variable. 
 


  1.  
  2. trace_info(): 
    The trace_info() method has replaced trace_vinfo() method and trace() method. It returns the name of the callback. This is generally used to find the name of the callback that is to be deleted. This method takes no argument other than the tkinter variable itself.
     
Syntax : trace_info(self)

  1.  


To better understand the significance of the above methods let's take an example and build a simple widget. Although using Tkinter variable as textvariable automatically updates the widget every time the variable changes but there might be times when the developer desires to do some extra processing while reading or modifying (or changing) the variable. This is where variable tracing comes in. Our callback function will trigger every time the text in the widget changes and will return a string "Variable Changed".
 

Python3
# Python program to trace# variable in tkinterfromtkinterimport*root=Tk()my_var=StringVar()# defining the callback function (observer)defmy_callback(var,index,mode):print("Traced variable{}".format(my_var.get())# registering the observermy_var.trace_add('write',my_callback)Label(root,textvariable=my_var).pack(padx=5,pady=5)Entry(root,textvariable=my_var).pack(padx=5,pady=5)root.mainloop()

Let's see the code in action where the trace_add() method registers an observer which gets triggered every time the textvariable in the widget changes.
Output:
 


 


Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp