Skip to content

Instantly share code, notes, and snippets.

@cppio
Created June 18, 2019 02:06
Show Gist options
  • Select an option

  • Save cppio/2d9555af9c5ad63469937e8dc6306b18 to your computer and use it in GitHub Desktop.

Select an option

Save cppio/2d9555af9c5ad63469937e8dc6306b18 to your computer and use it in GitHub Desktop.
from tkinter import ttk
class DebouncedEntry(ttk.Entry):
def __init__(self, master, delay, callback, validate=None):
super().__init__(master, validate="key")
self["validatecommand"] = self.register(self.validate), "%P"
self.delay = delay
self.callback = callback
self.validate = validate
self.alarm_id = None
def validate(self, entry):
if entry and self.validate and not self.validate(entry):
return False
if self.alarm_id is not None:
self.after_cancel(self.alarm_id)
self.alarm_id = None
if entry:
self.alarm_id = self.after(self.delay, self.callback, entry)
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment