2

I have a simple tkinter app:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

my_var = tk.IntVar()

slider = ttk.Scale(root, from_ = 0, to = 100, variable = my_var)
slider.pack()

label = ttk.Label(root, text = 'test', textvariable = my_var)
label.pack()

# run 
root.mainloop()

It works fine but the text displayed in the label is a floating point value, not an integer. This isn't that much of an issue but I am confused on a conceptual level: Shouldn't IntVar only store integers (i.e. convert floating point numbers when it stores them)? If it doesn't do it, what would be the point of DoubleVar?

Another_coder
  • 728
  • 1
  • 9
  • 23
  • if you use `my_var.get()` then you get integer value. It seems `label` gets original value without using `.get()` - probably it uses `my_var.trace()` for this. – furas Oct 04 '22 at 11:59
  • [How to retrieve the integer value of tkinter ttk Scale widget in Python? - Stack Overflow](https://stackoverflow.com/questions/38167774/how-to-retrieve-the-integer-value-of-tkinter-ttk-scale-widget-in-python) – furas Oct 04 '22 at 12:01
  • `IntVar` and `DoubleVar` are just a *wrapper classes* inside `tkinter` module with *override* `get()` method which returns value of the required type, they are not actually used by the underlying TCL interpreter. The underlying TCL interpreter just uses the *name* attribute inside the wrapper class to create a TCL variable inside the interpreter. Actually you can set any value (even a string like "abc") to a `IntVar` or `DoubleVar` without error. You can test by changing the line `my_var = tk.IntVar()` to `my_var = tk.IntVar(value="abc")` and the label will be shown with `"abc"` initially. – acw1668 Oct 05 '22 at 01:59

1 Answers1

0

Using DoublevVar.

import tkinter as tk
    
root = tk.Tk()
var = tk.DoubleVar()
scale = tk.Scale( root, variable = var )
scale.pack(anchor=tk.CENTER)


label = tk.Label(root, text = 'test', textvariable = var)
label.pack()

root.mainloop()

The result output:

enter image description here

toyota Supra
  • 3,181
  • 4
  • 15
  • 19