Reputation: 71
My program takes input in one screen and then goes and displays it in to another. I want to make a condition that the screen only changes when the value is digit greater than 0. My screen manager is defined in kivy file. My question is how to use this "root.manager.current = "SecondScreen"" command in my condition. I don't know how to reference it. Or maybe there is a way to make condition in .kv file?
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
my test.kv file
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}
Upvotes: 7
Views: 7597
Reputation: 5969
The method in kvlang:
app.root.current = 'screen_name_you_want_to_switch_to'
Can be done in python by using self.parent in your screen class:
self.parent.current = 'screen_name_you_want_to_switch_to'
self.parent refers to the parent object (screenmanager) of the self object (screen)
Example showing both is shown below, first a sidenote on your textinput: I think you may be able to access the value by using self.ids['text_input'].text
directly, you could try it.
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()
Upvotes: 9
Reputation: 41
One way to do this is to swap screens within the python file. Firstly define the screen manager simply as this with no properties or children (adding the screen will be done in python)
<ScreenManager>:
Add a ScreenManager class to the python file along with classes for FirstScreen and SecondScreen. Give ScreenManager() methods to swap in FirstScreen and 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)
Then add a method which is called by an on_press button event to execute some logic before showing a different screen, e.g.
def handle_button(self):
if <some logic>:
self.show_SecondScreen()
The button presses within the .kv file will have to reach up to the ScreenManager class to get to the handle_button method, something like root.parent.handle_button, depending on the depth of the structure
Upvotes: 2