python - AttributeError:“GridLayout”对象没有属性“print_text”
问题描述
我刚开始在 Kivy 中进行屏幕管理器编程。当我按下一个名为“Press Me!”的按钮时,我试图从 .kv 文件中调用一个函数“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
。
解决方案
该方法在 Python 脚本print_text()
中实现。class SampleScreen()
因此,在 kv 文件中,将子实例、类GridLayout:
规则替换为<ScreenOne>:
<ScreenTwo>:
SampleScreen:
片段 - kv
<ScreenOne>:
SampleScreen:
...
<ScreenTwo>:
SampleScreen:
推荐阅读
- r - 对齐分离图的尺寸
- react-native - CDN:trunk 相对路径:CocoaPods-version.yml 存在
- php - 如何在一个变量中存储多行,将其用作 android studio 中的 JSONArray
- sapui5 - webide模板创建的ui5项目中的一些npm包不能被npm审计
- build - 如何在 Build 机器上修复 Wix 错误 217
- java - Java 7 到 Java 8 JDK 增加了 CPU 使用率
- javascript - 我试图将列表视图放在滚动视图中,但它们不会聚在一起。我想显示一个项目列表并在底部添加一些文本
- python - 使用python在两个大型结构化点云中找到最近邻居的有效方法
- reactjs - React 在哪里定义辅助函数?
- c# - 个人 .config 文件在 C# Visual Studio 项目中不起作用