Description
Is your feature request related to a problem? Please describe.
I have a slider for my gui, but I don't want it to fire immediately until it stops moving. My quick solution to this is to have the app wait half a second before executing it, and cancel that request if a new one comes in. The downside to doing this method is that the console will spit out the warning Could not cancel function - it doesnt exist, it may have already run
every time I start moving the slider.
def slider_changed_for_real():
print('The new value is: ' + str(slider.value))
def slider_changed():
slider.cancel(slider_changed_for_real) # Throws a warning if `slider_changed_for_real` isn't scheduled.
slider.after(500, slider_changed_for_real)
Describe the solution you'd like
I'd like an optional parameter to cancel
that would not print the warning if set to True (defaults to False).
def cancel(self, function, ignoreWarning=False):
"""Cancel the scheduled `function` calls."""
if function in self._callback.keys():
callback_id = self._callback[function][0]
self.tk.after_cancel(callback_id)
self._callback.pop(function)
elif not ignoreWarning:
utils.error_format("Could not cancel function - it doesnt exist, it may have already run")
Either that or introduce a new method that returns a boolean if a function is scheduled or not.
def is_scheduled(self, function):
"""Returns True if the function has been scheduled."""
return function in self._callback.keys()
So as I'm writing this I figured out I can just do this to "solve" my problem:
def slider_changed():
# Only cancel if the function is scheduled. Avoids the warning.
if slider_changed_for_real in slider._callback.keys():
slider.cancel(slider_changed_for_real)
slider.after(500, slider_changed_for_real)
Not a fan of having to use a variable that is supposed to be private, so I'm still gonna advocate for an official solution. :)