Forum Archive

A constantly updating PYUI

AlanEdwards

Hi again,

On the second phase of my personal project I hope to be able to create a sequence clock, my example is a PYUI with two items - a field for a constantly running digital clock, and a start button. Eventually the screen will have different events announced on screen as their time approaches.

But as you may have guessed the clock only updates on each press of the button. Can anyone steer me? I'm hoping it is a quick fix, I can't get my head around .self.

Thanks, Alan


import ui
import time

def startSequence(sender):

    for loop in range(1,3):  # a test number
        currentTime=time.localtime(time.time())
        v['masterClock'].text=time.strftime("%H:%M:%S",currentTime)

# view of the UI is called here
v = ui.load_view('Sequence Timer')

v.present(orientations=['landscape'])
JonB

The trick is to use ‘ui.delay‘ to call an update method, which calls delay on itself. Don't try to update faster than about 0.1 sec. A threading.Timer also works.

Alternatively, a loop is possible but must use ‘ui.in_background‘ to call your loop method. I recommend delay.

JonB

See https://omz-forums.appspot.com/pythonista/post/5771444780793856

AlanEdwards

Thanks JonB, that last example from the link is exactly what I had hoped to have written, and more importantly it is written in a way I should be able to understand, not using those unfathomable classes or selfs. Love it.

JonB

Be sure to add a check inside that while True statement that your view is still on_screen, so the loop has a way of stopping.

Phuket2

This is what I did. Really, not sure it's a good approach or not. However it seems to work well.
Here goes nothing...preview looks wrong to what I am expecting

```python

import ui
import time

@ui.in_background
def start(sender):
if not sender.superview.on_screen : return

switch = sender.superview['switch']

'''
    setup controls, text labels etc..
'''

while True:
    if not sender.superview.on_screen:break 
    if not switch.value:continue 
    '''
        doing things here...

        if the switch is off pause, resume exectuing the while if the switch is switched back on.

        if the view closes,  then break to exit the while loop
    '''
    time.sleep(1)

```

Phuket2

Hmmm, sorry that post didn't go so well, not sure how to fix it!

AlanEdwards

Thanks Phuket2, the example JonB sent me worked straight out of the box, and I am sure yours would too, and if I may I will put it in my toolbox for later.

I learnt a hard-lesson on quoting code on this forum too –

It has to be three ticks, then on a new line 'python', then a blank line. Then finish with three more ticks.

Now can you help me on PYUI scrolling? I have a spreadsheet type view designed in the PYUI for the iPad consisting of rows and columns of individual textfields and action buttons, but do I have a clue if it can be made to scroll? No I do not.

ccc

The blank line needs to come before the <backtick><backtick><backtick>python line. You can go back and edit a previous post by clicking edit at the top right of your post.

Phuket2

Thanks for the help on posting guys. Alan, I am still a beginner, so sorry I can not help on the scrolling issue. I was trying to get many things to work on the tableview to scroll to the bottom of the list as I was adding to it in my background task. I tried many things, but didn't get a solution. It was not that important, so I didn't follow through. Also, I was only using ListDataSource, I didn't subclass. Still baby steps for me

Phuket2

Wow, I have been trying for ages to fix my post, I can't.... Seems so simple


Not working for me on my iPad. Brain is fried now will try again tomorrow.

Phuket2

Hmm, that last post give me a glue, maybe the markdown preview is not correct. Wil try one last time

dgelessus

Since it's basically impossible to properly explain Markdown code blocks when writing in Markdown, here's a short text on Pastebin explaining how to use code blocks. http://pastebin.com/UQ3gnPa5

Phuket2

I try again, exactly as it says on the link dgelessus. Still not working, I guess if I can't post here properly, no chance of me ever learning python. But I will persist. Maybe you can help me by just trying to copy my code and see if it works for you. Maybe there is something in my code that is making the problem. Maybe it's my iPad Air 2, I have no idea...

import ui
import time

@ui.in_background
def start(sender):
    if not sender.superview.on_screen : return

    switch = sender.superview['switch'] 

    '''
        setup controls, text labels etc..
    '''

    while True:
        if not sender.superview.on_screen:break 
        if not switch.value:continue 
        '''
            doing things here... 

            if the switch is off pause, resume exectuing the while if the switch is switched back on. 

            if the view closes,  then break to exit the while loop
        '''
        time.sleep(1)
Phuket2

Wow, that time it worked. Did nothing different. But 100% the markdown preview was wrong. I give in... I am in pattaya, thailand drinking... Python can take a back seat for a few hours!

dgelessus

Yes, the live preview doesn't support Python syntax highlighting, so if you use a ```python code block it appears broken in the preview, but will work once your submit the post.