AttributeError:“ GridLayout”对象没有属性“ print_text”

时间:2019-05-09 13:41:01

标签: python kivy

我只是从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()

预先感谢您的帮助

1 个答案:

答案 0 :(得分:0)

根本原因

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

解决方案

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

摘要-kv

<ScreenOne>:
    SampleScreen:

...

<ScreenTwo>:
    SampleScreen: