5

I have a slider and I want to call a specific function only when the interaction is complete. The idea is to call a function after 500ms (and not before). If the slider continues to move then the call is canceled. In other words, if the slider "rests" for more than 500ms than the function is call.

Thanks

Update

    #slider

    def updateValue(self):
        #self._job = None
        #self.preview.updateContourValue(float(self.slider.get() ))
        print "updated value"

    timer = Timer(5.0, updateValue)

    def sliderCallback(self):
        timer.cancel()
        timer.start()
aneuryzm
  • 63,052
  • 100
  • 273
  • 488

1 Answers1

2

Patrick: See this thread - How to create a timer using tkinter?

You can use Threading.Timer to do this. It has a cancel method that you can use to cancel it before it runs.

Community
  • 1
  • 1
aaronasterling
  • 68,820
  • 20
  • 127
  • 125
  • 1
    When interacting with a GUI library (which is what this sounds like), you probably don't want to use a thread. Use windowing timers. They won't run in a thread so they won't introduce threading issues; instead, they're run at some predefined point depending on the system. These are different in different GUI libraries, but they're a standard feature. For example, in Win32 it's `SetTimer()` and the WM_TIMER message. – Glenn Maynard Oct 22 '10 at 11:20
  • @aaronasterling I'm getting the following error message: raise RuntimeError("thread already started") (I've updated the question with my code) – aneuryzm Oct 22 '10 at 11:25
  • @Glenn Thanks for pointing that out. Will you put that as an answer so that it can be the accepted one? I'll leave this answer up as an object lesson. – aaronasterling Oct 22 '10 at 11:26
  • @Patrick, you're getting the runtime error because you can only call `start` once. After you cancel it, you need to start a new one but see Glenn's comment. If he puts an answer up, you should accept that. – aaronasterling Oct 22 '10 at 11:27
  • @aaronasterling Just to understand... so what's the point of using start() method ? Should I create a new Timer object every time instead ? – aneuryzm Oct 22 '10 at 11:31
  • @Patrick, you call the start method to start the Timer. You can only call it once though. After you call cancel, it's a dead thread and you need to create a new one. Glenn's (as of yet non-existent) answer is much better. – aaronasterling Oct 22 '10 at 11:41
  • @Glenn Maynard Ok so what's the default timer library for TKinter GUI on a mac ? – aneuryzm Oct 22 '10 at 11:45
  • it shows timer example, how to run only once after specific time (e.g., after 5 sec)? – user924 Apr 17 '18 at 10:29