22

I realize the short answer may be 'no,' but perhaps this is worth asking again.

If I am witting a Kivy app with a couple thousand of lines - then would it be possible to write some classes in another kv file?

This would make it so much easier to edit, correct errors, make changes... etc.

Just to clarify - the .KV files would be a continuation of each other - not pointing to a parallel app.

Some expert insight would be greatly appreciated - Thank you.

Edv Beq
  • 910
  • 3
  • 18
  • 43

3 Answers3

33

Yes it is! You can import .kv files inside files just like normal python files by starting with:

#:include otherfile.kv

If you want the file to unload and reload first you can force the import typing

#:include force otherfile.kv

instead.

All this as written in the Kivy Language Documentation which is full of useful clarifications.

Igor Jerosimić
  • 13,621
  • 6
  • 44
  • 53
Tadaboody
  • 458
  • 5
  • 8
  • 5
    I stopped messing with Kivy a long time ago - but I can see from the documentation that the option became available in 1.9 - a newer version since I asked the question. So in conclusion my question was not crazy. – Edv Beq Nov 29 '16 at 03:07
  • Do you know if you can navigate directories with this syntax? For example I can import stuff in python with say `from ui.myclass import MyClass`, but `#:include ui.mywidget.kv` doesn't work. The documentation doesn't seem to mention this. – Josh Jan 08 '17 at 02:06
  • 5
    @Josh Yes you can `#: include ui/mywidget.kv` – clfaster Dec 28 '17 at 11:12
8

Yes:

from kivy.lang import Builder
Builder.load_file('your_filename')
inclement
  • 29,124
  • 4
  • 48
  • 60
  • Ok, but my question is if I can load multiple kv files that work together as one. – Edv Beq Mar 30 '15 at 17:01
  • kv rules are standalone, as long as a rule is loaded before a widget is instantiated, the rule for that widget is applied. You can spread them over as many files as you want, though you can't break an individual kv rule over multiple files. – inclement Mar 30 '15 at 17:24
  • I understand the kv rules. I am only interested in loading mutltiple kv files. hypothetical example: if __name__ == "__main__": kv1App().run() kv2App().run() . . kv(n)App.run() Basically the python files reads classes from different kvs that point to the same 'interface.' Thank you – Edv Beq Mar 30 '15 at 19:23
  • I really don't understand the nature of your question. In your example, the apps will load kv files based on their names just as is normal, but they won't run simultaneously or anything. – inclement Mar 30 '15 at 19:24
  • 'but they won't run simultaneously or anything' - yes but python can run simultaneous processes – Edv Beq Mar 30 '15 at 19:26
  • Yes, of course, but you didn't even mention that until now and your example doesn't include them. I really am not sure what information you're trying to get. – inclement Mar 30 '15 at 19:30
  • Wait, are you asking not about kv files but about having multiple windows (via multiple App classes) for the same application? If that's the case, the answer is no, that's not possible. Well, you could run multiple apps at the same time and have them communicate, but kivy doesn't provide anything special to help with it. – inclement Mar 30 '15 at 19:32
  • ok sorry for not making myself clear - I will edit my first post to illustrate my question better. Thank your replies. – Edv Beq Mar 30 '15 at 19:34
  • @the last comment. All I am asking is if its possible to spread the information for 1 interface in (n) kv files - simply for better readability and editing purposes. – Edv Beq Mar 30 '15 at 19:36
  • And like I said the first time, yes, with Builder.load_file. If that's not what you want, explain what you mean by 'spread the information for 1 interface' – inclement Mar 30 '15 at 19:44
2

I believe you can create muliple .kv files and can include at one place (root file) by this way only you can distribute/branch your code Kivy documentation clearly says "A kv file must contain only one root widget at most" means only one kv main file. even if you run multiple instances of Mainapp class

I'm newer to the Kivy, so please correct me if I'm wrong. Thank you !