Skip to content

Instantly share code, notes, and snippets.

@fbstj
Created July 15, 2013 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fbstj/5999866 to your computer and use it in GitHub Desktop.
Save fbstj/5999866 to your computer and use it in GitHub Desktop.
a tkinter (ttk) button extension that repeatedly invokes the command whilst the button is held down, includes a simple exponential acceleration
from Tkinter import *
from ttk import *
class RepeatButton(Button):
def __init__(self, _, **kw):
Button.__init__(self, _)
self.config(**kw)
self.bind('<Button-1>', self._start)
self.bind('<ButtonRelease-1>', self._end)
def _start(self, event):
self._delay = 1500
self._on = True
self._do()
def _do(self):
if self._on:
self.invoke()
if self._delay > 100:
self._delay /= 2
self.after(self._delay, self._do)
def _end(self, event):
self._on = False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment