1

I am new to Kivy and need some help in understanding function scope. I have built a simple app with two screens. The first screen has two buttons and the second screen has a text label at the centre. On the first screen, I have used app.root.current='new_screen_name' with the on_release attribute for one button and it works fine. It takes me to the next screen which is the intended function. For the second button, I have used a function call which has been defined in the Python file under the class definition of the first screen (the root widget of the button). However, this method does not work and the app window simply closes. I guess I am making a mistake in the function scope and call But I cannot figure out what. Any help would be greatly appreciated.

Python file:

from kivy.config import Config
# Config.set should be used before importing any other Kivy module.
Config.set('kivy','window_icon','sivaicon.png')
# Config set for resizing image button
Config.set('graphics', 'resizable', True)
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import Button
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.lang.builder import Builder


class SivaLoginScreen(Screen):
    def func_authentication(self):
        app.root.current='tabbed_screen'


class SivaTabbedScreen(Screen):
    pass


class SivaScreenManager(ScreenManager):
    pass


class ImageButton(ButtonBehavior, Image):
    pass


# Tell Kivy to directly load a file. If this file defines a root widget, it will be returned by the method.
root_widget = Builder.load_file('siva.kv')

class SivaApp(App):
    def build(self):
        # Initialize root widget
        return root_widget


if __name__ == '__main__':
    # Run application
    SivaApp().run()

Kivy file (.kv):

SivaScreenManager:
    SivaLoginScreen:
    SivaTabbedScreen:


<ImageButton>:
    keep_ratio: True


<SivaLoginScreen>:
    name: 'login_screen'
    canvas.before:
        Color:
            rgba: 195/255, 60/255, 35/255, 1
        Rectangle:
            pos: self.pos
            size: self.size
    FloatLayout:
        size: root.width, root.height
        Image:
            id: login_logo_siva
            source: 'images/sivalogo4.png'
            keep_ratio: True
            size_hint: 0.2, 0.2
            pos_hint: {'center_x':0.5, 'center_y':0.75}
        Label:
            id: login_label_siva
            pos: self.x*0.5-4, self.y*0.5+15
            markup: True
            font_name: 'roboto/Roboto-Medium.ttf'
            text: '[color=#FDFD98]S.[/color][color=#B29DD9]I[/color][color=#FDFD98].[/color][color=#77DD77]V[/color][color=#FDFD98].[/color][color=#779ECB]A[/color]'
            font_size: '50sp'
        Label:
            id: login_label_slogan1
            pos: self.x*0.5-3, self.y*0.5-6
            markup: True
            font_name: 'roboto/Roboto-Regular.ttf'
            text: '[color=#FDFD98]SLOGAN TEXT[/color]'
            font_size: '15sp'
        Label:
            id: login_label_slogan2
            pos: self.x*0.5-3, self.y*0.5-20
            markup: True
            font_name: 'roboto/Roboto-Regular.ttf'
            text: '[color=#FDFD98]HEADLINE TEXT[/color]'
            font_size: '15sp'
        BoxLayout:
            id:login_button_layout
            orientation: 'horizontal'
            size_hint: 0.2, 0.2
            pos_hint: {'center_x':0.5, 'center_y':0.25}
            ImageButton:
                id: first_button
                source: {'normal': 'images/first.png', 'down': 'images/first-down.png'} [self.state]
                on_release: app.root.current='tabbed_screen'
            ImageButton:
                id: second_button
                source: {'normal': 'images/second.png', 'down': 'images/second-down.png'} [self.state]
                on_release: app.root.func_authentication()


<SivaTabbedScreen>:
    name: 'tabbed_screen'
    FloatLayout:
        size: root.width, root.height
        Label:
            pos: self.x*0.5, self.y*0.5
            text: 'SECOND SCREEN'
            font_size: '50sp'

1 Answer 1

2

In your case, app.root link to SivaScreenManager which is the root widget of your application. And in these class, there is not a func_authenticationfunction, why you app crashed.

To refer a class itself in a KV definition, you must just use root, so right code must be :

on_release: root.func_authentication()

see Kivy Language - Reserved Keywords

Definition of func_authentication is not correct also, app is unknown. Use either :

App.get_running_app().root.current='tabbed_screen' or

self.manager.current='tabbed_screen'

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your response but that did not work. Is the function definition correct for func_authentication() ? How can I change the current screen from inside the function?
Definition of func_authentication is not correct also, app is unknown. use App.get_running_app().root.current='tabbed_screen' or self.manager..current='tabbed_screen'
Thank you very much for helping me understand. That worked like a charm!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.