Forum Archive

Pythonista Countdown on button with .pyui

JeremyMH

How do I make a timer start to countdown with the click of a button? Here is an image of what I have so far. Im trying to make it so when you click "Launch", the timer starts. Can anyone help me? Thanks

mikael

@JeremyMH, if you have created this in the UI editor, now you have to move to the code.

You need to set the button’s action be a function that updates the timer and, if the timer should still be running, calls itself after a second with ui.delay.

JeremyMH

sorry im kind of a noob. can you please explain further! thanks

JeremyMH

like for example, how would i make it call the function and how woukd i change the value of the ui? i havent used python in a while.

ccc

@JeremyMH Can you please paste here the code that you have so far?

It is easier to debug Python code than it is to debug English prose.

mikael

@JeremyMH, let’s check the basics first.

  • Have you read the basic usage page?
  • Have you checked this section of the ui module manual and tried the example of tying a button to an action?
JeremyMH
import ui

def startlaunch(countdown):
    viewvalue = countdown.superview
    tapped = viewvalue['launchbutton']
    started = False
    if tapped:
        started = True
    while tapped: 
        ui.delay(countdowntick, 1)

def countdowntick():
    print("hi")

v = ui.load_view()
v.present('sheet')

but that just crashes it

mikael

@JeremyMH, it looks to me that your code should do nothing, not even give an error.

Did you try the code in my second link above?

enceladus

Look at the stopwatch1.py and stopwatch1.pyui example in the following GitHub repository. May be this is what you are looking for.
https://github.com/encela95dus/ios_pythonista_examples

BapeHiks

@JeremyMH a foundation vote that proposed @enceladus but without the editor UI

```
import ui
class StopWatch(ui.View):
def init(self, args, kwargs):
super().init(
args, **kwargs)
self.value = 0
self.state = 'stop'
self.update_interval = .1

def draw(self):
    t0 = (self.value // (600 * 60), self.value // 600, self.value // 10)
    t1 = (t0[0], t0[1] % 60, t0[2] % 60)
    ui.draw_string(
        "{:02}:{:02}:{:02}".format(*t1),
        font=('Helvetica', 20),
        rect=(150, 0, 0, 0),
        color='black',
        alignment=ui.ALIGN_CENTER)

def update(self):
    if self.state == 'run':
        self.value += 1
    self.set_needs_display()

def button_action(sender):
v1 = sender.superview['Watch']
sender.hidden = True
if sender.title == 'Reset':
v1.value = 0
v1.state = 'stop'
sender.superview['Start'].hidden = False
elif sender.title == 'Start':
v1.value = 0
v1.state = 'run'
sender.superview['Stop'].hidden = False
elif sender.title == 'Stop':
v1.state = 'stop'
sender.superview['Reset'].hidden = False

v = ui.View(width=397, height=271)

v.add_subview(StopWatch(name = 'Watch', frame = (0, 30, 345.00, 76.00)))
v.present('sheet')

for btn in ['Reset', 'Stop', 'Start']:
v.add_subview(ui.Button(frame=(v.center.x-40, v.center.y-40, 80, 80), name=btn, border_width=1, corner_radius=40, action=button_action))
v[btn].title = btn
if btn != 'Start':
v[btn].hidden = True```