简体   繁体   English

Python kv文件如何从另一个类调用函数

[英]Python kv file how to call function from another class

How can I call a method from class Account in my kv file?如何从我的 kv 文件中的 Account 类调用方法?

py file: 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 file: 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

Question #2问题2

if i add some money, how i update balance in Label lb2?如果我添加一些钱,我如何更新标签 lb2 中的余额?

Solution #2解决方案#2

Use Kivy Properties eg StringProperty, because they produce events such that when an attribute of your object changes, all properties that reference that attribute are automatically updated.使用 Kivy 属性,例如 StringProperty,因为它们会产生事件,这样当对象的属性发生变化时,所有引用该属性的属性都会自动更新。

Example #2示例#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


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()

MP.kv MP.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: 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()

Output #2输出#2

lb2 在余额更新时自动更新

Solution解决方案

Python Script - main.py Python 脚本 - main.py

  1. Add import statement for Kivy ObjectProperty eg from kivy.properties import ObjectProperty为 Kivy ObjectProperty 添加 import 语句,例如from kivy.properties import ObjectProperty
  2. Declare a Kivy ObjectProperty, eg acc = ObjectProperty(None)声明一个 Kivy ObjectProperty,例如acc = ObjectProperty(None)
  3. Implement constructor method for myWidget()为 myWidget() 实现构造函数方法

kv file kv文件

When the app start, acc is None.当应用程序启动时,acc 为 None。 Therefore, we need to check for None to avoid error.因此,我们需要检查 None 以避免错误。

text: '' if root.acc is None else 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


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()

MP.kv MP.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: '' 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

Output输出

输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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