如何从kv文件中的Account类调用方法?
py文件:
import kivy
kivy.require("1.10.1")
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.widget import Widget
class Account():
def __init__(self,name, balance):
self.name = name
self.__balance__ = balance
def getBalance(self):
return (self.__balance__)
def setBalance(self, zmena):
self.__balance__ = self.__balance__+zmena
acc = Account("Account1", "1000")
class myWidget(Widget):
Builder.load_file("MP.kv")
class MainApp(App):
def build(self):
return myWidget()
if __name__ == "__main__":
MainApp().run()
kv文件:
#:kivy 1.10.1
<Button>
background_color: 0.1, 0.1, 0.9, 0.9
font_size: 22
<Label>
background_color: 0.1, 0.1, 0.9, 0.9
font_size: 22
<myWidget>:
Label:
id: lb
text: "Account"
pos: root.width /2-self.width/2, root.top/2+200
Label:
id: lb1
text: "Account name"
pos: root.width /2-self.width/2, root.top/2+150
Label:
id: lb2
text: "balance" '''here i want call methot getBalance, but how?
pos: root.width /2-self.width/2, root.top/2+100
Label:
id: lb3
text: "Add/sub money"
pos: root.width /2-self.width/2, root.top/2+50
TextInput:
id: tp
text: "money"
pos: root.width /2-self.width/2, root.top/2-50
size_hint: .5, .25
Button:
id: btn1
text: "Confirm"
size_hint: .5, .25
pos: root.width /2-self.width/2, root.top/2-150
答案 0 :(得分:0)
如果我加了些钱,我如何更新标签lb2中的余额?
使用Kivy属性,例如StringProperty,因为它们会产生事件,这样,当对象的属性发生更改时,所有引用该属性的属性都会自动更新。
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
class Account():
def __init__(self, name, balance):
self.name = name
self.__balance__ = balance
def getBalance(self):
return (self.__balance__)
def setBalance(self, zmena):
self.__balance__ = self.__balance__ + zmena
class myWidget(Widget):
acc = ObjectProperty(None)
balance = StringProperty('')
def __init__(self, **kwargs):
super(myWidget, self).__init__(**kwargs)
self.acc = Account("Account1", 1008)
self.update_balance()
def update_balance(self):
self.balance = str(self.acc.getBalance())
Builder.load_file("MP.kv")
class MainApp(App):
def build(self):
return myWidget()
if __name__ == "__main__":
MainApp().run()
#:kivy 1.10.1
<Button>:
background_color: 0.1, 0.1, 0.9, 0.9
font_size: 22
<Label>:
background_color: 0.1, 0.1, 0.9, 0.9
font_size: 22
<myWidget>:
Label:
id: lb
text: "Account"
pos: root.width /2-self.width/2, root.top/2+200
Label:
id: lb1
text: "Account name"
pos: root.width /2-self.width/2, root.top/2+150
Label:
id: lb2
text: root.balance
pos: root.width /2-self.width/2, root.top/2+100
Label:
id: lb3
text: "Add/sub money"
pos: root.width /2-self.width/2, root.top/2+50
TextInput:
id: tp
hint_text: "money"
pos: root.width /2-self.width/2, root.top/2-50
size_hint: .5, .25
Button:
id: btn1
text: "Confirm"
size_hint: .5, .25
pos: root.width /2-self.width/2, root.top/2-150
on_release:
root.acc.setBalance(int(tp.text))
root.update_balance()
from kivy.properties import ObjectProperty
acc = ObjectProperty(None)
应用启动时,acc为None。因此,我们需要检查None以避免错误。
text: '' if root.acc is None else root.acc.getBalance()
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
class Account():
def __init__(self, name, balance):
self.name = name
self.__balance__ = balance
def getBalance(self):
return (self.__balance__)
def setBalance(self, zmena):
self.__balance__ = self.__balance__ + zmena
class myWidget(Widget):
acc = ObjectProperty(None)
def __init__(self, **kwargs):
super(myWidget, self).__init__(**kwargs)
self.acc = Account("Account1", "1000")
Builder.load_file("MP.kv")
class MainApp(App):
def build(self):
return myWidget()
if __name__ == "__main__":
MainApp().run()
#:kivy 1.10.1
<Button>:
background_color: 0.1, 0.1, 0.9, 0.9
font_size: 22
<Label>:
background_color: 0.1, 0.1, 0.9, 0.9
font_size: 22
<myWidget>:
Label:
id: lb
text: "Account"
pos: root.width /2-self.width/2, root.top/2+200
Label:
id: lb1
text: "Account name"
pos: root.width /2-self.width/2, root.top/2+150
Label:
id: lb2
text: '' if root.acc is None else root.acc.getBalance()
pos: root.width /2-self.width/2, root.top/2+100
Label:
id: lb3
text: "Add/sub money"
pos: root.width /2-self.width/2, root.top/2+50
TextInput:
id: tp
text: "money"
pos: root.width /2-self.width/2, root.top/2-50
size_hint: .5, .25
Button:
id: btn1
text: "Confirm"
size_hint: .5, .25
pos: root.width /2-self.width/2, root.top/2-150