Timers

Lesson 16: Timers

Removing matching timeouts

from fltk import *

def alarm_to(num):
    brow.add(str(num))

def rmatch_cb(wid):
    print("Remove matching timeouts: function name and user data")
    print(inp.value())
    Fl.remove_timeout(alarm_to, int(inp.value()) )


def rall_cb(wid):
    print("Remove all timeouts")
    Fl.remove_timeout(alarm_to)

w=Fl_Window(400,400)
w.begin()
rall=Fl_Button(120,20,100,30,"Remove all")
rmatch=Fl_Button(220,120,150,30,"Remove matching")
inp = Fl_Input(120,120,100,30,"User data")
brow = Fl_Browser(120,170,100,200)
w.end()

L = [0,1,1,2,3,3,4,4,5,5]
rall.callback(rall_cb)
rmatch.callback(rmatch_cb)
for x in range(len(L)):
    print(f'time: {1.0+(2*x)} function: alarm_to  user_data: {L[x]}')
    Fl.add_timeout(1.0+(2*x), alarm_to, L[x]) #1,3,5,7,9,11 secs
    # Note: L[x] user_data must be immutable

w.show()
Fl.run()

Dial timer

from fltk import *

def ticktock():
    time=int(display.value())
    time=time+1
    display.value(str(time))
    dial.value(dial.value()+6.0) # 6 degrees/sec
    Fl.repeat_timeout(1.0,ticktock)

def start_cb(widget):
    Fl.add_timeout(1.0, ticktock)
    butstart.deactivate()
    butstop.activate()

def stop_cb(widget):
    Fl.remove_timeout(ticktock)
    butstart.activate()
    butstop.deactivate()

def reset_cb(widget):
    dial.value(0.0)
    display.value('0')

w=Fl_Window(400,50, 300,300)
w.begin()
butstart=Fl_Button(20,20,70,30,"Start")
butstop=Fl_Button(20,100,70,30,"Stop")
butreset=Fl_Button(20,180,70,30,"Reset")
display=Fl_Output(100,100,70,30)
dial=Fl_Dial(150,170,90,90)
dial.type(FL_FILL_DIAL)
#dial.type(FL_LINE_DIAL)
dial.color(FL_WHITE,FL_RED)
dial.angle1(180)
dial.angle2(181)
w.end()

butstop.deactivate()
display.value('0')
butstart.callback(start_cb)
butstop.callback(stop_cb)
butreset.callback(reset_cb)
Fl.scheme('gleam') #plastic, gtk+, gleam, normal (default)
w.show()
Fl.run()