For an DoubleVar() 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 db1 from 5.1 to 10.34.
This triggers the trace() which used the callback function my_r() to print the value of the variable ( 10.34 ) to our console.
trace method is used to attach 'observer' callbacks to the variable
import tkinter as tk
from tkinter import *
my_w = tk.Tk()
my_w.geometry("300x100") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
print(db1.get()) # Print when variable changes.
db1 = tk.DoubleVar(my_w) # declare DoubleVar()
db1.set(5.1) # assign value to variable
b1 = tk.Button(my_w,text='Update',command=lambda:db1.set(10.34))
db1.trace('w',my_r) # callback when data changes
Use IntVar() for handling Integer data
Use DoubleVar() for handling Float Data
set() and get() methods of DoubleVar
In above examples we used set() method to assign data to the Double variable and to read the data stored we used get() method. These two methods are frequently used in our scripts.
db1.set(10.34) # assign value to db1
print(db1.get()) # display the value assigned to db1
We can using set() method to assign data to DoubleVar after declaring or we can assign value while declaring the string variable.
db1=tk.DoubleVar(value=5.1) # Assign value to db1
Length of DoubleVar
Before using len function, we have to convert the DoubleVar to string by using str()
Normal Variable and DoubleVar
DoubleVar() 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 DoubleVar we can monitor the changes and trigger functions based on the requirements.
Example : Calculating division of two numbers or multiplication of user entered decimal numbers requires float variables. Here DoubleVar is used if you want to track the changes in the variable.
StringVar()» BooleanVar()» IntVar()»