属性错误:"网格布局"对象没有属性"print_text"



我刚刚开始用Kivy进行屏幕管理器编程。print_text当我按下名为"按我!我是否像input.<function>?一样以错误的方式调用它,或者我是否对我的 python 代码做错了什么? 注意:我可以切换屏幕。

import kivy
kivy.require('1.10.0') # replace with your current kivy version !
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
Builder.load_string("""
<Manager>:
id: screen_manager
screen_one: screen_one
screen_two: screen_two
ScreenOne:
id: screen_one
name: "screen_one"
manager: screen_manager
ScreenTwo:
id: screen_two
name: "screen_two"
manager: screen_manager
<ScreenOne>:
GridLayout:
id: input
rows: 2
display: entry
orientation: "vertical"
spacing: 10
padding: 10
BoxLayout:
orientation: "horizontal"
Label:
text: "Input Your Text"
size_hint_x: 0.22
size_hint_y: 0.22
TextInput:
id: entry
size_hint_x: 0.78
size_hint_y: 0.22
BoxLayout:
orientation: "horizontal"
Button:
text: "Press Me!"
on_press:
input.print_text("Screen1:" + entry.text)
Button:
text: "SecondScreen"
on_press:
root.manager.transition.direction = "left"
root.manager.transition.duration = 1
root.manager.current = "screen_two"
<ScreenTwo>:
GridLayout:
id: input
rows: 2
display: entry
orientation: "vertical"
spacing: 10
padding: 10
BoxLayout:
orientation: "horizontal"           
Label:
text: "What's in your Mind?"
size_hint_x: 0.22
size_hint_y: 0.22
TextInput:
id: entry
size_hint_x: 0.78
size_hint_y: 0.22
BoxLayout:
orientation: "horizontal"
Button:
text: "Press Me!"
on_press:
input.print_text("Screen2:" + entry.text)
Button:
text: "FirstScreen"
on_press:
root.manager.transition.direction = "right"
root.manager.transition.duration = 1
root.manager.current = "screen_one"
""")
class SampleScreen(GridLayout):
def print_text(self, text):
print text
class ScreenOne(Screen):
pass
class ScreenTwo(Screen):
pass
class Manager(ScreenManager):   
pass
class SimpleApp(App):
def build(self):
return Manager()
myapp = SimpleApp()
myapp.run()

提前感谢您的帮助

根本原因

GridLayout没有调用的属性/方法,print_text

溶液

方法print_text()在 Python 脚本的class SampleScreen()中实现。因此,在 kv 文件中,将子实例、类规则GridLayout:<ScreenOne>:<ScreenTwo>:替换为SampleScreen:

片段 - kv

<ScreenOne>:
SampleScreen:
...
<ScreenTwo>:
SampleScreen:

最新更新