首页 > 解决方案 > 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()

在此先感谢您的帮助

标签: pythonkivy

解决方案


根本原因

GridLayout没有一个名为 的属性/方法print_text

解决方案

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

片段 - kv

<ScreenOne>:
    SampleScreen:

...

<ScreenTwo>:
    SampleScreen:

推荐阅读