Kivi - How to change text of label based on variable taken from mysql

Solution 1:

Try the following:

def build(self):
        self.sm = sm = ScreenManager()
        self.wid1 = wid1 = prono1(name='prono1')
        self.wid2 = wid2 = prono2(name='prono2')
        sm.add_widget(wid1)
        sm.add_widget(wid2)
        sm.ids['wid1']= wid1
        sm.ids['wid2']= wid2
        return sm

This way you'll be able to acces prono1 and prono2 instances from anywhere in your .py file.

Now, in the .kv file, set an id in the label you want to acces. For example:

        MDLabel:
            id: labl1   ## <------ Here you set the id
            size_hint_y: None
            size_hint_x: 1
            height: self.texture_size[1]
            text_size: self.width, None
            font_size: 20
            padding: 10, 20
            color: 0,0,0,1
            text: 'bla bla bla' #### <------------ name of the match that I want to display (WITH ID = 2 - so Roma - inter)
            halign: 'center'

To acces that label from .py file you have do:

App.get_running_app().sm.ids['wid1'].ids.labll

In the code above, ids['wid1'] is your prono1 instnace, and .ids.lbll refrences the label with the id:labl1

Now you're able to edit that label from anywhere in your program, for example if you want to edit that label in the function get_match then:

def get_match(self, id_partita):
        query = "SELECT partita FROM db.partite where id_partita = VALUES (%s)"
        values = (id_partita)
        c.execute(query, values)
        partita = c.fetchone() ## <---- In this variable I have the label that I want to display
        App.get_running_app().sm.ids['wid1'].ids.labll.text = 'Text has changed'
        return print(partita)