python - 使用来自其他 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:
解决方案
这实际上有点棘手。人们会期望您可以使用:
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
这会通过自动更新产生所需的结果。
推荐阅读
- python - RuntimeError: mat1 和 mat2 形状不能相乘
- elixir - elixir ecto - 创建和更新之间的变更集差异
- email - 如何允许程序访问 Google 帐户?
- ios - 如何在 Flutter 中使用 Xcode 12.5 解决 Stripe 支付问题?
- google-sheets - 我正在寻找一个使我的数字随机化直到它们总和在一个范围内的公式
- javascript - 带有文件夹的 Imgurr 图像上传
- matlab - 如何使用差异功能
- kubernetes - cluster-info ConfigMap 尚不包含令牌 ID“cjxj26”的 JWS 签名
- node.js - 集成nodejs api来flutter
- hive - 在 HIVE 中删除酸表失败