首页 > 解决方案 > 通过屏幕管理器kivy回收输入值

问题描述

我想从我的第一个屏幕捕获一个值到我的第三个屏幕。

首先,我在输入字段中写下我的名字。我去下一个窗口。

我尝试在最后一个窗口中显示我的名字。

所以我和你分享代码,我希望我能找到一个问题。

蟒蛇代码:

import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import ScreenManager, Screen



#define ou different screens
class FirstWindow(Screen):
    def envoyer(self):
        name = self.ids.nom_input.text

        self.ids.my_name.text = name


class SecondWindow(Screen):
    pass

class ThirdWindow(Screen):
    #PROBLEM HERE
    def on_press(self):
        self.ids.recup_infos.text = self.root.get_screen('FirstWindow').ids.my_name.text


class WindowManager(ScreenManager):
    pass

class MonWidget(Widget):
    pass


kv = Builder.load_file('new_window.kv')

class AwesomeApp(App):

    def build(self):
        Window.clearcolor = (0,0,0,0)
        return kv

    
if __name__ == '__main__':
    AwesomeApp().run()

我的 KV 代码:

WindowManager:
    FirstWindow:
    SecondWindow:
    ThirdWindow:




<FirstWindow>:
    name: "romain"

    BoxLayout:
        orientation: "vertical"
        size: root.width, root.height

        Label:
            id: my_name
            text: "Entrez votre nom"
            font_size: 32

        TextInput:
            id: nom_input
            multiline: False
            size_hint: (1, .5)

        Button:
            text: "Next screen"
            font_size: 32
            on_press: root.envoyer()
            on_release:
                app.root.current = "Mickael"
                root.manager.transition.direction = "left"
            


<SecondWindow>:
    name: "Mickael"

    BoxLayout:
        orientation: "vertical"
        size: root.width, root.height

        Label:
            text: "Entre votre ville"
            font_size: 32

        TextInput:
            id: ville_input
            multiline: False
            size_hint: (1, .5)


        Button:
            text: "Vérifier les infos"
            font_size: 32
            on_release:
                app.root.current = "foncier"
                root.manager.transition.direction = "left"

        Button:
            text: "go back first screen"
            font_size: 32
            on_release:
                app.root.current = "romain"
                root.manager.transition.direction = "right"

    


<ThirdWindow>:
    name: "foncier"

    BoxLayout:
        orientation: "vertical"
        size: root.width, root.height

        Label:
            text: "Verifier : "
            font_size: 32


        Label:
            id: recup_infos
            text: ""
            font_size: 32
            color: 'white'


        Button:
            text: "On press"
            font_size: 32
            #Problem HERE
            on_press: root.on_press()

        Button:
            text: "Précedent"
            font_size: 32
            on_release:
                app.root.current = "Mickael"
                root.manager.transition.direction = "right"

你可以帮帮我吗 ?谢谢罗曼

标签: pythonpython-3.xkivykivy-language

解决方案


在你的on_press方法中:

def on_press(self):
    self.ids.recup_infos.text = self.root.get_screen('FirstWindow').ids.my_name.text

self.root.get_screen('FirstWindow').ids.my_name.text不是访问您现在所在的班级之外的小部件的正确方法,或者在这种情况下,屏幕。正确的方法是使用App.get_running_app()方法:

self.ids.recup_infos.text = App.get_running_app().root.ids.First.ids.my_name.text

但在此之前,您必须为应用程序的屏幕提供 id,以便First上面演示的方法的参数实际上是有意义的:

WindowManager:
    FirstWindow:
        id: First 
        # "First" is the id of the FirstWindow class
        # which can also explain why there was a "First" arg
        # inside "App.get_running_app().root.ids.First.ids.my_name.text"
    SecondWindow:
        id: Second
    ThirdWindow:
        id: Third

仍然对为什么这样做感到困惑?让我们将 的属性App.get_running_app().root.ids.First.ids.my_name.text分为 3 个部分:

  • App.get_running_app():此方法返回您正在运行的 App 类的位置,在本例中为AwesomeApp。这也就像self您要在 App 对象本身中获取变量一样
  • .root.ids.First:如果你仔细阅读 Kivy 文档,或者只是简单地在线观看 Kivy 课程视频,你应该知道self.root.ids在 App 对象内部会返回根小部件内小部件的 id 列表。在这种情况下,在App.get_running_app().root.ids这里做同样的事情,并且您的屏幕在ScreenManager根小部件中传递,因此FirstApp.get_running_app().root.ids
  • .ids.my_name.text:与上面App.get_running_app().root.ids.First相同,self就像您在FirstWindow课堂上运行它一样,这使您有机会访问工作课程/屏幕之外的变量

推荐阅读