Kivy - How to trigger ModalView from Python code

219 views Asked by At

I want to force the start of a ModalView when the app is loaded, but i can not figure out how to do it.

It's working fine when i push the button, but i cant figure out how to trigger the event from the python file.

How can i from the code trigger the event (button)?

KV file

<Controller>

    <BoxLayout>

        ***a lot of code ***

        Button:
            id: my_id
            text: "text"
            color: txt_color
            size_hint_y: .05
            background_color: btn_color_not_pressed if self.state=='normal' else btn_color_pressed

            on_release:
                Factory.About().open()

<About@ModalView>
    id: about
    auto_dismiss: True

    
    *** some more code ****

How do i call the event from my main.py?


After solution from @john Anderson:

file: main.py

import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout

from kivy.factory import Factory


kivy.require("1.11.1")


class Controller(BoxLayout):
    def __init__(self):
        super(Controller, self).__init__()

        Factory.About().open()

class mainApp(App):
    def build(self):
        return Controller()


mainApp().run()

file: main.kv

    #:import Factory kivy.factory.Factory


<Controller>
    BoxLayout:
        orientation: "vertical"
        Label:
            text: "THIS IS THE MAIN TEKST"
            color: 1,0,0,1
            size_hint_y:.8
        Button:
            text: "About"
            size_hint_y: .2
            on_release: Factory.About().open()

<About@ModalView>

    size_hint: .8,.5

    BoxLayout
        orientation: "vertical"
        size: self.width, self.height
        pos_hint: {"center_x": .5}
        size_hint: .8,.6


        Label:
            text: "ABOUT MY APP"
            color: 0,1,0,1
        Button:
            text: "Back"
            size_hint_y: .2
            on_release: root.dismiss()

1

There are 1 answers

0
John Anderson On

Your About ModalView is being created in the __init__() method of your root Controller widget. That means that it is created before your Controller. An easy fix is to delay the creation of the About widget until after the App is started. You can use the App method on_start() to do that:

class Controller(BoxLayout):
    pass


class mainApp(App):
    def build(self):
        return Controller()

    def on_start(self):
        Factory.About().open()