0

How to disable the callback to control variable when hovering (not clicking) the mouse above the slider (not trough) of the tkinter Scale widget, after clicking on/dealing with other widget(s)?

Demo procedure:
1.) Run demo script below
2.) Click button '(Re-) Set DoubleVar to float value'
3.) Watch the (python) Shell for callback from tracing the control variable when hovering the FIRST time the mouse above the slider after setting the DoubleVar to float.
4.) Potentially repeat step 2.) and 3.)

import tkinter as tk

def func_set():
    dvar.set(1.23456789)

def traceCallback(var, index, mode):
    print('\nTraceback: ', var)
    print('DoubleVar value: ', dvar.get())

# Demo script    
root = tk.Tk()
root.title('Demo of hovering effect')

mainframe = tk.Frame(root)
mainframe.grid(sticky='NSWE', padx=50, pady=50)

# tk.DoubleVar
dvar = tk.DoubleVar()
dvar.set(2)
# Trace changes on DoubleVar
dvar.trace_add('write', traceCallback)
# Scale widget
scale = tk.Scale(mainframe, from_=0, to_=5, length=500, orient='horizontal', resolution=0.5, variable=dvar)
scale.grid(sticky='NSWE')

# Button widgets
button_set = tk.Button(mainframe, command=func_set, text='(Re-) Set DoubleVar to float value')
button_set.grid(column=0, row=1)

root.mainloop()

I'd like to bind the tkinter Scale widget to a tkinter DoubleVar (float) variable, but having a defined increment (e.g. 0.5) and displaying the rounded value accordingly on the widget.
Any suggestions?

I'm using Python 3.8+ on Windows 10.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
AG77
  • 1
  • 2
  • This callback happens because the value does not match the steps of the slider and if you hover over it, it will snap to the next suitable value. The most easy way to get rid of it would be to set only values the slider accepts. – Klaus D. Jun 16 '23 at 08:38
  • When moving/clicking the slider I want exactly this rounding of the value, but not when just hovering above the slider. – AG77 Jun 16 '23 at 09:13
  • Besides, the variable must be a float for other reasons. I'd just like to display a defined number of digits and have the increment configured accordingly. – AG77 Jun 16 '23 at 09:22
  • This [question](https://stackoverflow.com/questions/24838629/round-off-float-to-nearest-0-5-in-python) may help. – acw1668 Jun 20 '23 at 02:02

0 Answers0