Last active
January 19, 2021 00:21
-
-
Save tshirtman/d8423695fe821440b65688b8dbd22b0c to your computer and use it in GitHub Desktop.
A function to load kv files automatically.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def load_kv(): | |
'''This magical function lookup module name, and load the kv file | |
with the same name (in the same directory) | |
''' | |
filename = inspect.currentframe().f_back.f_code.co_filename | |
f = extsep.join((splitext(filename)[0], 'kv')) | |
if exists(f) and f not in Builder.files: | |
Builder.load_file(f) | |
Your gist looks like an answer to something I couldn't find in the official Kivy documentation but I couldn't have it run. I get the error
NameError: name 'inspect' is not defined
Thanks for it though.
inspect is a python module, you have to import it.
Interesting
Nice! I am using
import pathlib
Builder.load_file(f'{pathlib.Path(__file__).parent.absolute()}{pathlib.os.sep}{pathlib.Path(__file__).stem}.kv')
in my python files. Works for python 3.7+ AFAIK.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example usage.
testclass.py
testclass.kv