3

I understand that you don't need a variable to be specified for a scale like such:

scale = tk.Scale(root, from_ = 1, to = 100) # makes scale without variable
scaleValue = scale.get() # sets value to scale

I, however, need a way to have a variable set in real time and every time the scale value is changed. Is there a way to make that work without constantly resetting scaleValue to scale.get()?

GeeTransit
  • 1,458
  • 9
  • 22
  • 1
    You might be able to do by using a tkinter variable class and assigning it to the `Scale` widget via its `variable=` configuration option. Here's a little documentation on the [variable classes](http://effbot.org/tkinterbook/variable.htm) to get you started. Also see [Control variables: the values behind the widgets](http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/control-variables.html). The tricky part will be doing it in real time because tkinter doesn't support concurrency, so you'll need to communicate via queues or something similar between the GUI and whatever is producing the data. – martineau Nov 12 '18 at 07:13
  • Possible duplicate of [Tkinter Python Scale retrieving integer value](https://stackoverflow.com/questions/38167774/tkinter-python-scale-retrieving-integer-value) – stovfl Nov 12 '18 at 07:59
  • I'm guessing that `scaleValue = tk.IntVar()` should do the trick, right? – GeeTransit Nov 12 '18 at 14:07
  • @GeeTransit I added an update to my example to also include a `DoubleVar()`. To use this correctly you will also need to set the resolution to be a float like this: `resolution=0.01` – Mike - SMT Nov 12 '18 at 15:14
  • Ah. The slider can output either a float or an int depending on the resolution, where resolution is 1 by default. Currently I just need integer output, but now I know how to get float inputs. Thanks :D – GeeTransit Nov 12 '18 at 16:31

2 Answers2

1

By making a tkinter variable using variable = tk.DoubleVar(), it will automatically update variable when a change has happened.

scaleVar = tk.DoubleVar
scale = tk.Scale(
    root,
    from_ = 1,
    to = 100,
    variable = scaleVar    # makes scale with updating variable
)
GeeTransit
  • 1,458
  • 9
  • 22
  • `BoolVar` is not a thing. That said`BooleanVar()` would still not be useful here as it will just return true. – Mike - SMT Nov 12 '18 at 14:18
  • Shoot I meant tk.DoubleVar()... Looks like I got a brain fart. – GeeTransit Nov 12 '18 at 14:32
  • DoubleVar is better. I think IntVar is best here as it is going to return an Integer and your slider does not need to return a float. DoubleVar returns a float. – Mike - SMT Nov 12 '18 at 14:44
  • I've heard that sliders return a float, meaning it will convert the slider value to an int if I am using `tk.IntVar()`, correct? – GeeTransit Nov 12 '18 at 15:00
  • Well the slider never displays a float. Maybe it is an option that needs to be set for it. Here is a post on the Int vs Double. [Tkinter Scale and floats when resolution > 1](https://stackoverflow.com/questions/25361926/tkinter-scale-and-floats-when-resolution-1) – Mike - SMT Nov 12 '18 at 15:07
1

If you use something like an IntVar() to track the value you can see that it is auto updated with a function that will check the current value.

If you want the value to be displayed and returned as a float you can use DoubleVar() and then also set resolution=0.01 as an argument in the Scale widget.

import tkinter as tk

class Example(tk.Tk):
    def __init__(self):
        super().__init__()
        self.int_var = tk.IntVar()
        self.scale = tk.Scale(self, from_=1, to=100, variable=self.int_var)
        self.scale.pack()

        tk.Button(self, text="Check Scale", command=self.check_scale).pack()

    def check_scale(self):
        print(self.int_var.get())


if __name__ == "__main__":
    Example().mainloop()

Results:

enter image description here

For an example using the DoubleVar() you can do this:

import tkinter as tk

class Example(tk.Tk):
    def __init__(self):
        super().__init__()
        self.dou_var = tk.DoubleVar()
        self.scale = tk.Scale(self, from_=1, to=100, resolution=0.01, variable=self.dou_var)
        self.scale.pack()

        tk.Button(self, text="Check Scale", command=self.check_scale).pack()

    def check_scale(self):
        print(self.dou_var.get())


if __name__ == "__main__":
    Example().mainloop()

Results:

enter image description here

Mike - SMT
  • 14,784
  • 4
  • 35
  • 79