eric
eric

Reputation: 41

Change label text acc. to python variable

I am writing code for a heating control system.

I just want to be able to change the label texts FROM WITHIN PYTHON. By that I mean, NOT in the GUI code, but somewhere else in the main.

Here is my MWE:

import time
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
import multiprocessing 
from kivy.properties import StringProperty


class Data (Widget):
    top = StringProperty('hiii')
    def __init__(self, **kwargs):
        super(Widget, self).__init__(**kwargs)


global mydata     
mydata=Data()

class myw (BoxLayout):
    def __init__(self, **kwargs):
        super(myw, self).__init__(**kwargs)
        VERT = BoxLayout(orientation='vertical')
        o = Label(text='Oben: ',
                  font_size=120)
        m = Label(text='Mitte: ',
              font_size=120)
        u = Label(text='Unten: ',
              font_size=120)
        a = Label(text='Aussen: ',
              font_size=120)

        mydata.bind(top=o.setter('text'))

       VERT.add_widget(o)
       VERT.add_widget(m)
       VERT.add_widget(u)
       VERT.add_widget(a)

       onoff = Button(text='Ein',
              font_size=120,
              size_hint=(0.3, 1))
       self.add_widget(VERT)
       self.add_widget(onoff)



class TutorialApp(App):
    def build(self):
        return myw()

if __name__ == "__main__":
    try:
        global myapp
        myapp=TutorialApp()
        app_runner=multiprocessing.Process(target=myapp.run)
        app_runner.start()
        time.sleep(3)
        mydata.top='new value assigned'
        print (mydata.top)
        time.sleep(5)
        app_runner.terminate()
    except Exception as e:
        print ('error occured', e)

I deliberately declared the variable 'mydata' outside the kivy code, such that i can access it from elsewhere in the code (not shown here).

Upvotes: 1

Views: 137

Answers (1)

eric
eric

Reputation: 41

Using threading instead of multiprocessing solved the problem.

So instead of

app_runner=multiprocessing.Process(target=myapp.run)

it now reads:

app_runner=threading.Thread(target=myapp.run)

Upvotes: 1

Related Questions