Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change screen in Python if Screen Manager is defined in kivy file?

Tags:

python

kivy

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}
like image 201
user5500849 Avatar asked Nov 21 '15 12:11

user5500849


People also ask

How do I make multiple windows on Kivy?

Kivy isn't great at allowing you to create multiple windows. But what it DOES allow you to do is create multiple “screens” inside your main window, and that's what we'll do in this video. We'll use ScreenManager to manage our different screens, and I'll show you how to scroll between them with the click of a button.


1 Answers

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()
like image 52
Sebastian Avatar answered Nov 10 '22 00:11

Sebastian