【发布时间】:2016-02-23 22:19:22
【问题描述】:
我的程序在一个屏幕上接受输入,然后将其显示到另一个屏幕上。我想设定一个条件,即屏幕仅在值大于 0 时才会改变。 我的屏幕管理器在 kivy 文件中定义。 我的问题是如何在我的情况下使用这个 "root.manager.current = "SecondScreen"" 命令。我不知道如何引用它。 或者也许有办法在 .kv 文件中创建条件?
main.py
from kivy.app import App
from kivy.properties import StringProperty, NumericProperty, ObjectProperty
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.textinput import TextInput
from kivy.properties import StringProperty, NumericProperty, ObjectProperty
global var
global pp
pp = '0'
class FirstScreen(Screen):
global pp
ppp = StringProperty('')
def g(self, text):
global pp
if text.isdigit() and int(text) > 0:
pp = text
我的 test.kv 文件
ScreenManager:
id: screen_manager
FirstScreen:
id: screen1
name: "FirstScreen"
manager: screen_manager
SecondScreen:
id: screen2
name: "SecondScreen"
manager: screen_manager
<FirstScreen>:
text_input: text_input
FloatLayout:
id: fl1
canvas.before:
Color:
rgba: 0.1, 0.1, 0.1, 1
Rectangle:
# self here refers to the widget i.e BoxLayout
pos: self.pos
size: self.size
TextInput:
id: text_input
text: root.ppp
multiline: False
size_hint_x: .4
size_hint_y: .1
pos_hint: {'x': .1, 'y': .20}
Button:
background_color: 0.2, 0.7, 1, 1,
font_size: root.width / 15
id: btn1
text: "Next"
on_press:
root.g(text_input.text)
root.manager.current = 'SecondScreen'
size_hint_x: .4
size_hint_y: .1
pos_hint: {'x': .5, 'y': .20}
<SecondScreen>:
on_enter: root.f()
FloatLayout:
id: fl2
canvas.before:
Color:
rgba: 0.1, 0.1, 0.1, 1
Rectangle:
# self here refers to the widget i.e BoxLayout
pos: self.pos
size: self.size
Label:
color: 0.2, 0.7, 1, 1,
font_size: 15
id: lb1
text: root.idf
size_hint_y: .2
pos_hint: {'x': .1, 'y': .8}
【问题讨论】: