@every untill....



  • Hello!

    I recently found out about Tingbot, and this is looks awesome! I was just wondering if I could, for example, show the time untill someone pressed the left button...

    Something like this:

    @every(seconds=1.0/30) untill left_button.press
    
    def loop():
        
        currentDate = time.strftime("%d %B %Y")
        currentTime = time.strftime("%H:%M:%S")
        
        screen.fill(color='black')
        
        screen.text(currentTime, xy=(160,110), color="white", font_size=50)
        screen.text(currentDate, xy=(160, 180), color="white", font_size=24)
    
    @left_button.press
        #code I want to do when the left button is pressed
    

    Thanks in advance,
    EagleX21

    EDIT: Or is there like a function that can quit the @every?



  • # code block
    import tingbot
    from tingbot import *
    import time
    
    
    showTime = False
    
    @left_button.press
    def press():
        global showTime
        showTime = not showTime
    
    @every(seconds=1.0/30)
    def drawTime():
        if showTime: 
            currentDate = time.strftime("%d %B %Y")
            currentTime = time.strftime("%H:%M:%S")
            screen.fill(color='black')
            screen.text(currentTime, xy=(160,110), color="white", font_size=50)
            screen.text(currentDate, xy=(160, 180), color="white", font_size=24)
        else:
            screen.fill(color='black')
            screen.text('Waiting for a button...')
    
    
    tingbot.run()
    

  • Core team

    Nice one A-999! Also, there's another way to make repeating functions that doesn't use the decorator - create_timer.

    Here's an example:

    import tingbot
    from tingbot import *
    import time
    
    def drawTime():
        currentDate = time.strftime("%d %B %Y")
        currentTime = time.strftime("%H:%M:%S")
        
        screen.fill(color='black')
        
        screen.text(currentTime, xy=(160,110), color="white", font_size=50)
        screen.text(currentDate, xy=(160, 180), color="white", font_size=24)
    
    draw_timer = create_timer(drawTime, seconds=1/30.0)
    
    @left_button.press
    def stopDrawingTime():
        draw_timer.stop()
    
    tingbot.run()
    

Log in to reply

Looks like your connection to @every untill.... was lost, please wait while we try to reconnect.