简体   繁体   中英

Kivy Label not updating

So I'm new to kivy and i build this but i don't understand why it won't update. When its printed, the list are updating, but not the label itself.

main.py

class MasukTol(Screen):
    ubahintsaldo = [str(int) for int in nyimpen.saldoawal]
    textsaldo = ''.join(ubahintsaldo)
    n = 0

    def masukkansaldo(self):
        self.instruksimasuk = "Silakan masukan \n saldo Anda!"
    def updatesaldo(self):
        self.ubahintsaldo = [str(int) for int in nyimpen.saldoawal]
        self.ids.textsaldo = ''.join(str(nyimpen.saldoawal))
        return self.ids.textsaldo, self.ubahintsaldo

def pencetsatu(self):
        if self.n < 6:
            nyimpen.saldoawal[self.n]=1
            self.n +=1
            self.updatesaldo()
            print(nyimpen.saldoawal)
            print(self.n)
            return nyimpen.saldoawal, self.n

kv file

<MasukTol>:
        Label:
            id: Saldo
            text: root.textsaldo


        # Nomor 1
        Button:
            background_normal: "photos/atm_1.png"
            background_down: "photos/atm_down_1.png"
            allow_stretch: True
            keep_ratio: False
            size_hint: 0.11, 0.141
            pos_hint:{"x":root.kiri_atm, "y":root.atas_atm}
            on_release:
                root.pencetsatu()

aand i made a seperate python file for keeping track of every thing.

nyimpen.py

saldoawal = [0,0,0,0,0,0]

Alfreda

i changed your code a little bit:

alfreda.py

from kivy.app import App
    from kivy.lang import Builder
    from kivy.uix.screenmanager import Screen
    from kivy.properties import ListProperty, StringProperty, NumericProperty
    
    
    class nyimpen:
        saldoawal = [0, 0, 0, 0, 0, 0]
    
    
    class MasukTol(Screen):
        counter = NumericProperty(0)
        instruksimasuk = StringProperty()
        ubahintsaldo = StringProperty()
        saldo_value = StringProperty()
    
        def __init__(self):
            super(MasukTol, self).__init__()
            self.ubahintsaldo = str([str(int) for int in nyimpen.saldoawal])
            self.saldo_value = ''.join(self.ubahintsaldo)
    
        def masukkansaldo(self):
            self.instruksimasuk = "Silakan masukan \n saldo Anda!"
    
        def updatesaldo(self):
            self.ubahintsaldo = str([str(int) for int in nyimpen.saldoawal])
            self.saldo_value = ''.join(str(nyimpen.saldoawal))
            return self.saldo_value, self.ubahintsaldo
    
        def pencetsatu(self):
            if self.counter < 6:
                nyimpen.saldoawal[self.counter] = 1
                self.counter += 1
                self.updatesaldo()
                print(nyimpen.saldoawal)
                print(self.counter)
                return nyimpen.saldoawal, self.counter
    
        def on_counter(self, *args):
            print('counter', self.counter)
    
        def on_ubahintsaldo(self, *args):
            print('ubahintsaldo', self.ubahintsaldo)
    
        def on_saldo_value(self, *args):
            print('saldo_value',  self.saldo_value)
    
    
    class AlfredaApp(App):
        def build(self):
            Builder.load_file('./alfreda.kv')
            return MasukTol()
        pass
    
    
    if __name__ == '__main__':
        AlfredaApp().run()
        pass

alfreda.kv

        <MasukTol>:
        id: masuk_tol
        Label:
            id: Saldo
            text: root.saldo_value
    
    
        # Nomor 1
        Button:
            # background_normal: "photos/atm_1.png"
            # background_down: "photos/atm_down_1.png"
            text: str(root.counter)
            allow_stretch: True
            keep_ratio: False
            size_hint: 0.11, 0.141
            # pos_hint:{"x":root.kiri_atm, "y":root.atas_atm}
            on_release:
                root.pencetsatu()

as inclement mentioned: thank's to the kivy StringProperty the code now does what you expected. https://kivy.org/doc/stable/api-kivy.properties.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM