Forum Archive

Provide button press feedback

marturo

Hello,

I'm working on a view where I have several buttons and a text above them. Every time the user presses a button a line is saved to a csv file with the button that was pressed and the time. The on top of the screen changes every time a button is pressed, but sometimes the text is so similar to the previous one that it's hard to see the change.

The entire program is working fine; however, it's hard to see whether the button was actually pressed.

I tried to add the following piece of code to the action function to change the color of the button temporarily to gray and back, but it doesn't work:

sender.bg_color = 'gray'
time.delay(0.25)
sender.bg_color = 'green'

It seems that the screen only refreshes after the button has changed back to green.

Is there a way to force a screen refresh after I change the color to gray? or is there a better way to provide a visual cue that the button has been pressed and the action has taken place?

Thank you.

marturo

I'm sorry I meant:

time.sleep(0.25)

cvp

Perhaps

View.set_needs_display()
marturo

@cvp I tried that, but it still doesn't refresh the screen. I don't know if it makes any difference, but I'm working with a subview

cvp

Try this

import ui
class myview(ui.View):
    def __init__(self):
        b = ui.Button(name='button_name')
        b.frame=(10,10,32,32)
        b.title = 'test'
        b.background_color = 'green'
        b.action = self.button_tapped
        self.add_subview(b)
    def button_tapped(self,sender):
        sender.background_color = 'gray'
        ui.delay(self.button_tapped_end,1)
    def button_tapped_end(self):
        self['button_name'].background_color = 'green'

v = myview()    
v.present() 

marturo

That did the trick! Thank you.

JonB

Also, the original code would have worked with a @ui.in_background decorator. Nothing in a callback shows up until the method ends and control is returned to the ui thread. Though the issue with in_background is that all such calls are queued up on the same thread, so if you tapped the button 20 times in a row, it would take many seconds to toggle through all of these.

In this case it probably does not matter, but in some cases you might want a ui.cancel_all_delays() at the start of button_tapped, o avoid calling the end method 20 times.