Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I would like to use .cancel() without a warning. #513

Open
DavidSM64 opened this issue Dec 11, 2024 · 0 comments
Open

I would like to use .cancel() without a warning. #513

DavidSM64 opened this issue Dec 11, 2024 · 0 comments

Comments

@DavidSM64
Copy link

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. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant