Dynamically update Beeware Toga widgets from outside the class

239 Views Asked by At

In my Beeware app I did not find a way to return results to the user on android, by dynamically updating an output value in a TextInput box . I am looking for a way to dynamically update the toga widgets, in particular text widgets. In my search I encountered a short discussion at https://github.com/beeware/toga/discussions/1325 , from which it appears that the thing should be possible, and even easy. The discussion at the link above includes a short exemplary code, but I had no success in trying to make it work for me. I would much appreciate any clarification how to adapt that code to Beeware app, or how to update the text widgets in any other way.

UPDATE: I finally found out that what caused the crash in android was the keyword refresh() that works with windows but not with android. The following code works OK with TextInput on both the Beeware emulator and on my Galaxy23 phone. However does not work with MultilineTextInput

import toga
from toga.style import Pack
from toga.style.pack import COLUMN, ROW
import time
import threading

class HelloWorld(toga.App):

    def change(self,text):
        self.name_input.value=text

    def startup(self):
        main_box = toga.Box(style=Pack(direction=COLUMN))
        self.name_input = toga.TextInput(value=text,style=Pack(flex=1),readonly=True)
        name_box = toga.Box(style=Pack(direction=ROW, padding=5))
        name_box.add(self.name_input) 

        main_box.add(name_box)

        self.main_window = toga.MainWindow(title=self.formal_name)
        self.main_window.content = main_box
        self.main_window.show()

def thr():
    global text,change
    text="start text"
    time.sleep(2)
    for i in range(5):
        text="new text "+str(i)
        myApp.change(text)
        time.sleep(1)

def main():
    global myApp
    myApp=HelloWorld()
    return myApp

t1=threading.Thread(target=thr,args=());t1.start()
0

There are 0 best solutions below