我的程序在一个屏幕上输入,然后将其显示在另一个屏幕中。我想提出一个条件,即只有当数值大于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}
答案 0 :(得分:4)
kvlang中的方法:
app.root.current = 'screen_name_you_want_to_switch_to'
可以在屏幕类中使用self.parent在python中完成:
self.parent.current = 'screen_name_you_want_to_switch_to'
self.parent指自身对象(屏幕)的父对象(screenmanager)
显示两者的示例如下所示,首先是文本输入的旁注:我认为您可以直接使用self.ids['text_input'].text
来访问该值,您可以尝试。
import kivy
kivy.require('1.9.0')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager,Screen
kvlang = '''
<ScreenManagement>:
ScreenOne:
ScreenTwo:
<ScreenOne>:
name: 'First'
id: screen1
Button:
text: 'screen 1, press me to switch to screen 2, defined in python'
on_press: root.switch()
<ScreenTwo>:
name: 'Second'
id: screen2
Button:
text: 'screen 2, press me to switch to screen 1, defined in kvlang'
on_press: app.root.current = 'First'
'''
class ScreenManagement(ScreenManager):
pass
class ScreenOne(Screen):
def switch(self):
#here you can insert any python logic you like
self.parent.current = 'Second'
class ScreenTwo(Screen):
pass
class MyApp(App):
def build(self):
Builder.load_string(kvlang)
return ScreenManagement()
if __name__ == '__main__':
MyApp().run()
答案 1 :(得分:1)
执行此操作的一种方法是在python文件中交换屏幕。首先将屏幕管理器简单地定义为没有属性或子项(添加屏幕将在python中完成)
<ScreenManager>:
将ScreenManager类与FirstScreen和SecondScreen的类一起添加到python文件中。给ScreenManager()方法交换FirstScreen和SecondScreen
class ScreenManager(widget):
def __init__(self):
self.show_FirstScreen()
def show_FirstScreen(self):
firstscreen = FirstScreen()
self.clear_widgets()
self.add_widget(firstscreen)
def show_SecondScreen(self):
secondscreen = SecondScreen()
self.clear_widgets()
self.add_widget(secondscreen)
然后添加一个on_press按钮事件调用的方法,在显示不同的屏幕之前执行某些逻辑,例如
def handle_button(self):
if <some logic>:
self.show_SecondScreen()
按下.kv文件中的按钮必须到达ScreenManager类才能获得handle_button方法,类似于root.parent.handle_button,具体取决于结构的深度