Forum Archive

Time.sleep() for ui?

Bjucha

Hello everybody, trying to create a simple timer that counts down from 10 to 0.
Creating one in that executes in console is no problem when using time.sleep function and a simple while condition, but for the ui if using a time.sleepfunction everything freezes. Read someware that time.sleep does not work for the ui. Is that correct? And if so what should I use instead?

ccc

omz-software.com/pythonista/docs/ios/ui.html#ui.delay

Bjucha

@ccc thx!

Bjucha

Hmm wrote it like this

import time, ui

timme = 30

def CountDown(sender):
    global timme

    if timme > 0:
        label1.text = str(timme)
        ui.delay(sender.CountDown, 1)
        timme = timme - 1
        label1.text = str(timme)

but now I get "Attribute error" ui.Button has no attribute 'CountDown' and it refering to the

ui.delay(sender.CountDown, 1)

line. Tried different combinations for the ui.delay but will always get this error. What am I missing?

ccc

In the line that is failing, sender is a ui.Button but you have defined CountDown as a standalone function, not as a method of ui.Button().

Try CountDown(sender) instead of sender.CountDown()

Also code format requires the three backticks are on a line by themselves with no other text before or after them on the same line.

semonbright

Sleep() function actually suspends the processing of the thread in which it is called by the operating system, allowing other threads and processes to execute while it sleeps. With multiple threads and processes, sleep() suspends your thread - it uses next to zero processing power.

hanfuontod

Carry on, don’t stop posting like this