3

I am trying to make an app. I have got it to work where all my files are in the same folder but it gets very messy and I would like to separate my files into separate folders of the structure: Start, Prelogin, andFirebaseLoginScreen`. So is what I am trying to do possible in kivy?

I have tried #: import name x.y.z as the pointer where x is the folder name, y is the name of kv-file and z is the class in kv-file I want to import, but I get so many weird errors when I try to do it. I have pretty much added every folder and everything to my PYTHONPATH but nothing works.

Start contains main.py and main.kv where main.kv then points to the screenmanger in ``Prelogin. Prelogin contains some files that consist of labels and text about the app and then points to the screenmanger in FirebaseLoginScreen. The FirebaseLoginScreen contains a lot of files for the login system.

Ganesa Vijayakumar
  • 2,422
  • 5
  • 28
  • 41
  • Remember that for Python to recognize a folder, it must contain an `__init__.py` file (can be empty).. – noEmbryo Nov 03 '19 at 18:49

2 Answers2

2

Yes, I'will give you and example with this folder structure

-Project\
--main.py
--main.kv
--folder1\
----window1.py
----window1.kv
--folder2\
----window2.py
----window3.py
--folder3\
----window4.py
----window4.kv

folder\window1.py will be like

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang.builder import Builder
from kivy.uix.widget import Widget

kivy.require("1.11.1")
# Builder is neccesary to work with multiple files
Builder.load_file("folder1/window1.kv")

class login(BoxLayout):    
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

class login_window(App):        
    def build(self):            
        return login() 

if __name__=="__main__":    
    aplicacion=login_window()
    aplicacion.run()

folder1\window1.kv is not necessary to specify.

main.py will be like

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang.builder import Builder
from kivy.uix.widget import Widget

# Import files like
from folder1.window1 import login_window
from folder2.window2 import example2_window
from folder3.window3 import example3_window

class manager(BoxLayout):

    # Add screens to main
    login_widget=login_window()
    example2_widget=example2_window()
    example3_widget=example3_window()        

    def __init__(self, **kwargs):            
        super().__init__(**kwargs)

        # Check kv file to understand these lines
        self.ids.screen_login.add_widget(self.login_widget)
        self.ids.screen_example2.add_widget(self.example2_widget)
        self.ids.screen_example3.add_widget(self.example3_widget)            

class main(App):
    def build(self):
        return manager()

if __name__ == "__main__":
    main().run()

main.kv will be like (Which has the ScreenManager!)

#:kivy 1.11.1
<manager>:
    id: main_window

    ScreenManager:
        id: scrn_mngr_main

        Screen:
            id: screen_login
            name:'screen_login'
        Screen:
            id: screen_example2
            name:'screen_example2'
        Screen:
            id: screen_example3
            name:'screen_example3'       

Now, to control the program flow yo need to add these lines in window1.py

        self.parent.current='screen_login'
        self.parent.current='screen_example2'
        self.parent.current='screen_example3'

Adapted from 3 videos of a tutorial on Youtube

Pablo Díaz
  • 330
  • 4
  • 14
0

The answer is simply "yes", there's nothing special about the import parsing in kv language, or in Python when using Kivy. If you're having issues, it's because what you're attempting is wrong or insufficient, which isn't possible to debug from the amount of information you've given.

If you'd like to follow it up, post another question with an example that you think should work given your PYTHONPATH manipulations.

inclement
  • 29,124
  • 4
  • 48
  • 60