首页 > 解决方案 > 使用来自其他 kivy 类的条目

问题描述

我是语言初学者,我正在尝试制作一个简单的猜谜游戏,我想知道如何使用 Start_p1 类的 Players 类中输入的数据。我希望在 Players 类的 TextInput 中键入的名称出现在 Start_p1 类的 Label Text: 中。

Arquivo .py:

import kivy
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.textinput import TextInput
from kivy.properties import StringProperty

class Home(Screen):
    def next(self):
        self.manager.current = "players"

class Players(Screen): 
    def start(self):
        self.manager.current = 'st1' 


class Start_p1(Screen):
    def runn(self):
        self.manager.current = 'st2'


class Start_p2(Screen):

    def back(self):
        self.manager.current = 'st1'

class Finish(Screen): pass

class Myapp(App):
    sm = None

    def build(self):
        self.sm = ScreenManager()
        self.sm.add_widget(Home(name = 'home'))
        self.sm.add_widget(Players(name = 'players'))
        self.sm.add_widget(Start_p1(name = 'st1'))
        self.sm.add_widget(Start_p2(name = 'st2'))
        self.sm.add_widget(Finish(name = 'finish'))
        self.sm.current = 'home'
        return self.sm


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

Arquivo.kv

#: import utils kivy.utils


<Home>:
    FloatLayout:
        canvas.before:
            Color:
                rgb: utils.get_color_from_hex('#2169af')
            Rectangle:
                pos: self.pos
                size: self.size
        Button:
            text: 'Iniciar'
            pos_hint: {'center_x': .5, 'center_y': .5}
            size_hint: 0.2, .1
            background_color: utils.get_color_from_hex('#13447d')
            on_release: root.next()

        Button:
            text: 'Configurações'
            pos_hint: {'center_x': .5, 'center_y': .4}
            size_hint: 0.2, .1
            background_color: utils.get_color_from_hex('#13447d')

<Players>:
    FloatLayout:
        canvas.before:
            Color:
                rgb: utils.get_color_from_hex('#2169af')
            Rectangle:
                pos: self.pos
                size: self.size

        Label:
            pos: 0, 270
            text: 'JOGADORES'

        Label:
            pos: 0, 150
            text: 'Informe o nome do 1° Jogador:'
            TextInput:
                id: txtt
                pos: 270, 400
                size: 250, 30
                multiline: False

        Label:
            pos: 0, -20
            text: 'Informe o nome do 2° Jogador:'
            TextInput:
                id: txt
                pos: 270, 230
                size: 250, 30
                multiline: False

        Button:
            text: 'Iniciar'
            pos_hint: {'center_x': .5, 'center_y': .2}
            size_hint: 0.2, .1
            background_color: utils.get_color_from_hex('#13447d')
            on_release: root.start()

<Start_p1>:
    Players:
        id: players
    FloatLayout:
        canvas.before:
            Color:
                rgb: utils.get_color_from_hex('#2169af')
            Rectangle:
                pos: self.pos
                size: self.size
        Label: 
            text:

标签: pythonpython-3.xkivy

解决方案


这实际上有点棘手。人们会期望您可以使用:

Label:
    text: app.sm.get_screen('players').ids.txt.text

text这在技术上是可行的,但如果发生TextInput更改,它不会设置自动更新。仔细阅读文档解释了原因,并提出了解决方法。使用文档中的建议,您可以执行以下操作:

Label:
    players_screen: app.sm.get_screen('players')
    text: self.players_screen.ids.txt.text

这会通过自动更新产生所需的结果。


推荐阅读