detecting keyboard overrun in tkinter event handler when autorepeating to avoid lag
<p>In my app I want to allow the user to scroll through images by holding down an arrow key. Not surprisingly with larger images the pc can't keep up, and builds up a potentially large buffer that carries on being processed after the key is released.</p>

<p>None of this is unexpected, and my normal answer is just to check the timestamp in the event against the current time and discard any events that are more than (say) .2 seconds old. This way the backlog can never get too large.</p>

<p>But tkinter uses some random timebase of events so that comparing with time.time() is meaningless, and I can't find a function to get hold of tkinter's own clock. I'm sure its in there, it's just most of the pythonised tkinter documentation is a bit naff, and searching for time or clock isn't helping either.</p>

<pre><code>def plotprev(self,p):
    if time.time() - p.time &gt; .2:
        return
</code></pre>

<p>Sadly this test always returns true, where is tkinter's pseudo clock to be found?
Any other method will be complex in comparison.</p>

<p>well it's nor very nice, but it isn't too tedious and seems to work quite well: (with a little bit of monitoring as well)</p>

<pre><code>    def checklag(self,p):
    if self.lasteventtime is None: #assume first event arrives with no significant delay
        self.lasteventtime = p.time
        self.lasteventrealtime = time.time()
        self.lagok=0
        self.lagfail=0
        return True
    ptdiff = (p.time-self.lasteventtime) / 1000
    rtdiff = time.time() - self.lasteventrealtime
    lag = rtdiff-ptdiff
    if lag &lt; .3:
        self.lagok += 1
        if self.lagok %20 == 0:
            print("lagy? OK: %d, fail: %d" %(self.lagok, self.lagfail))
        return True
    else:
        self.lagfail += 1
        return False
</code></pre>

python events tkinter