Pythonista-Issues icon indicating copy to clipboard operation
Pythonista-Issues copied to clipboard

Change label.text but no Update

Open kami83 opened this issue 5 months ago • 15 comments

Hi,

i am changing the "label1.text" with this function, but on my view it is not updated? Can you tell me how to activate an update of the view?

Thanks a lot.

BR kami

` def swswitch (sender): if main['connectsw'].value == True:

        main['label1'].text = " Waiting..."
        #main.update()
        cb.scan_for_peripherals()
        count = time.time()
        
        while not mngr.peripheral and count >= time.time()-10: 
            
            pass
            
        if mngr.peripheral == None:
            main['label1'].text = " Device not found..."
            main['connectsw'].value = False
            cb.stop_scan()

    else:
        #v['viewbottom']['label1'].text = " Disconnected..."
        if mngr.peripheral != None:
            cb.cancel_peripheral_connection(mngr.peripheral)
    pass  

`

kami83 avatar Jul 31 '25 07:07 kami83

Try

from objc_util import main_thread

@main_thread
def swswitch (sender):

cvpe avatar Jul 31 '25 14:07 cvpe

Hi thanks but main_thread is Not defined. BR kami

kami83 avatar Jul 31 '25 14:07 kami83

Sorry, my error

from objc_util import *

@on_main_thread

cvpe avatar Jul 31 '25 15:07 cvpe

No Problem but now it is defined but no Update. 🫤

kami83 avatar Jul 31 '25 16:07 kami83

Last try, instead of previous bad advice

@ui.in_background
def swswitch (sender):

cvpe avatar Jul 31 '25 17:07 cvpe

Same Problem

kami83 avatar Jul 31 '25 17:07 kami83

weird, sure that your code is executed, as it is in an "if", perhaps your could print something to confirm

cvpe avatar Jul 31 '25 17:07 cvpe

If you agree for a test more 🙄

@on_main_thread
def update_label(text):
	main['label1'].text = text

@ui.in_background
def swswitch (sender):
	if main['connectsw'].value == True:
		update_label("" Waiting...")
		...

cvpe avatar Jul 31 '25 17:07 cvpe

Hi test failed 🫤

kami83 avatar Jul 31 '25 17:07 kami83

I can't help more, sorry, you have to be sure that your code is executed. Put a print in the update_label def.

cvpe avatar Jul 31 '25 17:07 cvpe

The Print command in def update_Label is executed but now the Label.text also with "device not found" is not updating. This works before.

kami83 avatar Jul 31 '25 17:07 kami83

Sorry for you, hoping a Pythonista guru will help you. Do you use the beta version or the App Store version?

cvpe avatar Jul 31 '25 17:07 cvpe

It is normal that your original code was only displaying the last text of the label because your def is entirely executed before the label could be updated.

cvpe avatar Jul 31 '25 17:07 cvpe

Okay but thanks a lot for your help. I am using the Beta.

kami83 avatar Jul 31 '25 18:07 kami83

Perhaps is the beta the origin of the problem...

cvpe avatar Jul 31 '25 20:07 cvpe