如何绑定kv和py?

时间:2018-03-24 05:44:52

标签: python kivy kivy-language

我是Kivy和Python的新手,我遇到了绑kv代码和py代码的困难。 在这里,我从启动画面进入登录菜单。我在kv中使用TextInput询问孩子的姓名和年龄,并尝试用py代码打印,但是我犯了这个错误:

PrintData() takes 2 positional arguments but 3 were given

我认为我犯了一些愚蠢的错误或者选择了一种错误的方式来组织代码。

我的代码:

from kivy.app import App


from kivy.lang import Builder

from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput

Builder.load_string('''
<RootScreen>:
    transition: FadeTransition()
    IntroScreen:
    NameScreen:

<IntroScreen>:
    AnchorLayout:
        Image:
            source: 'yCFD2.png'
            size_hint: 1,1
        Button:
            background_color: [63, 191, 63, 0.0]
            text: ''
            on_press: root.manager.current = 'data'

<NameScreen>:
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: 'X6TF0.png'
    FloatLayout:
        TextInput:
            id: ChildName
            size_hint:.2, .2
            pos_hint:{'x': 0.7, 'y': 0.4}
            text: "Введи имя"
            focus: True
            multiline: False
        TextInput:
            id: ChildAge
            size_hint:.2, .2
            pos_hint:{'x': 0.7, 'y': 0.6}
            text: "Введи возраст"
            focus: True
            multiline: False
        Button:
            size_hint:.2, .2
            pos_hint:{'x': 0.7, 'y': 0.8}
            background_color: [63, 191, 63, 0.3]
            text: 'Добавить в базу'
            on_press: root.PrintData(ChildName, ChildAge)
''')


class IntroScreen(Screen):
    def intro(self):
        pass

class NameScreen(Screen):
    ChildName = StringProperty()
    ChildAge = StringProperty()

    def __init__(self, **kwargs):
        super(NameScreen, self).__init__(**kwargs)
        self.ChildName = ''

    def __init__(self, **kwargs):
        super(NameScreen, self).__init__(**kwargs)
        self.ChildAge = ''

    def PrintData(ChildName, ChildAge):
        print(ChildName, ChildAge)


sm = ScreenManager()
sm.add_widget(IntroScreen(name='intro'))
sm.add_widget(NameScreen(name='data'))

class SampleApp(App):
    def build(self):
        return (sm)

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

1 个答案:

答案 0 :(得分:0)

解释

传递给 on_&lt; property_name&gt; 事件的第一个参数或绑定到该属性的函数是 self ,这是定义此函数的类的实例。

解决方案

请参阅以下代码段和解决方案示例。

片段

Python脚本

def censor(text, word):
  words = text.split()
  print words
  result = ''
  stars = '*' * len(word)
  count = 0
  for i in words:
      if i == word:
          words[count] = stars
      count += 1
  result =' '.join(words)

  return result

kv文件

def PrintData(self, ChildName, ChildAge):
    print(ChildName, ChildAge)

实施例

main.py

on_press: root.PrintData(ChildName.text, ChildAge.text)

sample.kv

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import StringProperty


class IntroScreen(Screen):
    def intro(self):
        pass


class NameScreen(Screen):
    ChildName = StringProperty()
    ChildAge = StringProperty()

    def __init__(self, **kwargs):
        super(NameScreen, self).__init__(**kwargs)
        self.ChildName = ''

    def __init__(self, **kwargs):
        super(NameScreen, self).__init__(**kwargs)
        self.ChildAge = ''

    def PrintData(self, ChildName, ChildAge):
        print(ChildName, ChildAge)


class RootScreen(ScreenManager):
    pass


class SampleApp(App):
    def build(self):
        return RootScreen()


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

输出

Img01 - App Startup Img02 - Child Name & Age Img03 - Print Child Name & Age