Skip to content Skip to sidebar Skip to footer

Python Kv File How To Call Function From Another Class

How can I call a method from class Account in my kv file? py file: import kivy kivy.require('1.10.1') from kivy.app import App from kivy.lang import Builder from kivy.uix.widget im

Solution 1:

Question #2

if i add some money, how i update balance in Label lb2?

Solution #2

Use Kivy Properties e.g. StringProperty, because they produce events such that when an attribute of your object changes, all properties that reference that attribute are automatically updated.

Example #2

main.py

import kivy

kivy.require("1.10.1")
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty, StringProperty


classAccount():
    def__init__(self, name, balance):
        self.name = name
        self.__balance__ = balance

    defgetBalance(self):
        return (self.__balance__)

    defsetBalance(self, zmena):
        self.__balance__ = self.__balance__ + zmena


classmyWidget(Widget):
    acc = ObjectProperty(None)
    balance = StringProperty('')

    def__init__(self, **kwargs):
        super(myWidget, self).__init__(**kwargs)
        self.acc = Account("Account1", 1008)
        self.update_balance()

    defupdate_balance(self):
        self.balance = str(self.acc.getBalance())


Builder.load_file("MP.kv")


classMainApp(App):
    defbuild(self):
        return myWidget()


if __name__ == "__main__":
    MainApp().run()

MP.kv

#:kivy 1.10.1<Button>:background_color:0.1,0.1,0.9,0.9font_size:22<Label>:background_color:0.1,0.1,0.9,0.9font_size:22<myWidget>:Label:id:lbtext:"Account"pos:root.width/2-self.width/2,root.top/2+200Label:id:lb1text:"Account name"pos:root.width/2-self.width/2,root.top/2+150Label:id:lb2text:root.balancepos:root.width/2-self.width/2,root.top/2+100Label:id:lb3text:"Add/sub money"pos:root.width/2-self.width/2,root.top/2+50TextInput:id:tphint_text:"money"pos:root.width/2-self.width/2,root.top/2-50size_hint:.5,.25Button:id:btn1text:"Confirm"size_hint:.5,.25pos:root.width/2-self.width/2,root.top/2-150on_release:root.acc.setBalance(int(tp.text))root.update_balance()

Output #2

lb2 automatically updated when balance is updated

Solution

Python Script - main.py

  1. Add import statement for Kivy ObjectProperty e.g. from kivy.properties import ObjectProperty
  2. Declare a Kivy ObjectProperty, e.g. acc = ObjectProperty(None)
  3. Implement constructor method for myWidget()

kv file

When the app start, acc is None. Therefore, we need to check for None to avoid error.

text: ''if root.acc isNoneelse root.acc.getBalance()

Example

main.py

import kivy

kivy.require("1.10.1")
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty


classAccount():
    def__init__(self, name, balance):
        self.name = name
        self.__balance__ = balance

    defgetBalance(self):
        return (self.__balance__)

    defsetBalance(self, zmena):
        self.__balance__ = self.__balance__ + zmena


classmyWidget(Widget):
    acc = ObjectProperty(None)

    def__init__(self, **kwargs):
        super(myWidget, self).__init__(**kwargs)
        self.acc = Account("Account1", "1000")


Builder.load_file("MP.kv")


classMainApp(App):
    defbuild(self):
        return myWidget()


if __name__ == "__main__":
    MainApp().run()

MP.kv

#:kivy 1.10.1<Button>:background_color:0.1,0.1,0.9,0.9font_size:22<Label>:background_color:0.1,0.1,0.9,0.9font_size:22<myWidget>:Label:id:lbtext:"Account"pos:root.width/2-self.width/2,root.top/2+200Label:id:lb1text:"Account name"pos:root.width/2-self.width/2,root.top/2+150Label:id:lb2text:''ifroot.accisNoneelseroot.acc.getBalance()pos:root.width/2-self.width/2,root.top/2+100Label:id:lb3text:"Add/sub money"pos:root.width/2-self.width/2,root.top/2+50TextInput:id:tptext:"money"pos:root.width/2-self.width/2,root.top/2-50size_hint:.5,.25Button:id:btn1text:"Confirm"size_hint:.5,.25pos:root.width/2-self.width/2,root.top/2-150

Output

output

Post a Comment for "Python Kv File How To Call Function From Another Class"