我是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()
解释
传递给on\lt;property\u name>;事件或绑定到属性的函数的第一个参数是self,它是定义此函数的类的实例。你知道吗
解决方案
请参考以下代码片段和示例以了解解决方案。你知道吗
片段
Python脚本
kv文件
示例
你知道吗主.py你知道吗
你知道吗样品.kv你知道吗
输出
相关问题 更多 >
编程相关推荐