简体   繁体   中英

Kivy dynamically replace kv file and add .widget

Here i'm trying to refresh the screen based on a variable named "view". The goal is to load the header and footer and the requested view (page) The view doesn't change when i call View('example') but all prints works well.

What i am missing? I'm new with python and kivy so if there is something that already exists for that, i'll take it:)

main.py

import kivy
import runpy
import re
import pathlib
from kivy.app import App
from libraries.env import env
from App.Controllers.Controller import View

class App(App):

    route = ""
    params = {}

    def build(self):
        return View('home')

    #Parse routes.py and load the required controller
    def route(self, route, params=None):
        filepath = 'routes.py'
        controller = ""
        controllerClass = ""
        action = ""
        with open(filepath) as fp:
            for line_num, line in enumerate(fp):
                splitted = re.split('=|@', line)
                if splitted[0] == route :
                    controller = splitted[1].replace('"', '')
                    controllerClass = controller.split('/')[-1]
                    action = splitted[2].replace('"', '').replace('\n', '')
        if controller:
            runpy.run_path(controller + '.py', run_name='__index__')
        else:
            print("Le controller pour la route " + route + " est introuvable.")


if __name__ == '__main__':
    App().run()

App/Controllers/HomeController.py

from App.Controllers.Controller import View

class HomeController():
    
    def index(self):
        print('Controller : HomeController => index')
        return View("home")

if __name__=='__index__':
    HomeController().index()

App/Controllers/Controller.py

import pathlib
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout

class View(BoxLayout):

    def __init__(self, view, **kwargs):
        print('View : pages/' + view + '.kv')
        super(View, self).__init__(**kwargs)
        self.clear_widgets()
        self.orientation = "vertical"
        self.add_widget(Builder.load_file("App/Views/partials/header.kv"))
        p = pathlib.Path('App/Views/pages/'+view+'.kv')
        if p.is_file():
            self.add_widget(Builder.load_file("App/Views/pages/"+view+".kv"))
            print("Chargement de la vue ok.")
        else:
            print("La vue " + view + " est introuvable.")
        self.add_widget(Builder.load_file("App/Views/partials/footer.kv"))

More globally I'm trying to learn python (I come from PHP). I train by making a small home-made framework. The work in progress is available here: https://github.com/mchev/Larathon

The view doesn't change when i call View('example') but all prints works well.

That's because this instantiates a new View but doesn't do anything with it, so your code runs but you don't display the result anywhere.

Presumably you intended to run some code on an existing View, not a new one like this. Post a minimal runnable example demonstrating the problem if you can't work out how to do so.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM