Kivy class in .py and .kv interactions 2
Follow the Kivy class in .py and .kv interactions , but more complex.
Here is the complete code of what I am writing:
data / screens / learnkanji_want.kv has how I want the code to be, but I don't quite understand how the class KanjiOriginScreen()
plays a role in controlling the screen.
data / screens / learnkanji.kv works the way I want it, but for that I need to put keyb_height
in a class KanjiOriginScreen()
( main.py >). However, I want this code to be in a class LayoutFunction()
( learnkanji.py ).
Question
How can I put keyb_height
in a function LayoutFunction()
and access this in a file .kv
in <LayoutFunction>
?
Could you also explain why KanjiOriginScreen:
it is possible to put in learnkanji.kv without < >
and the program still recognizes that it should use this?
If anything is unclear, ask :)
Edit
I found out that I had not imported learnkanji.py into the learnkanji.kv file and that caused it to be unable to find the LayoutFunction class.
#:import learnkanji data.screens.learnkanji
source to share
Sorry I didn't understand my question, but using IRC on #Kivy I ended up with the following:
learnkanji.py
class LayoutFunctioning(BoxLayout):
keyb_height = NumericProperty(260)
learnkanji.kv
KanjiOriginScreen:
name: 'LearnKanji'
fullscreen: True
LayoutFunction:
id: lfunc
#...code...
height: lfunc.keyb_height #Instead of root.keyb_height
Now I understand how to use id, I can use lfunc to call my code in LayoutFunction () :)
source to share
To answer your questions:
-
The way you do it should work. You must be able to access the attributes of the objects from kv. However, if your attribute changes and you want the UI to update when it does, you should use Kivy Properties. If it is persistent, the normal attribute works fine.
-
From Kivy Docs
<Widget>:
- This is a class rule that will apply to all instances of that class.Widget:
will create an actual instance of this class (in this case, your root widget).
As far as ScreenManager and screens are concerned, you can think of them this way. Each screen is its own individual user interface (own root widget). A screen manager is a container that contains your screen and can change between different screens. This allows you to create separate user interfaces with which you can switch between them. Each user interface is a separate tree of widgets with a screen at its root. The docs do a pretty good job of describing ScreenManager.
source to share
How can I put keyb_height in LayoutFunction () and access that in .kv file?
You cannot do this with a function. To do this, you need to make LayoutFunction in class
. For example:
main.py
class LayoutClass(BoxLayout): # I made it a boxlayout, you could make it anything you want
keyb_height = NumericProperty(260) # from kivy.properties import NumericProperty
kv file:
<LayoutClass>: # can only access it this way if it a class in main.py
something: root.keyb_height
Could you also explain why KanjiOriginScreen: can be put in learnkanji.kv without <> and the program will still recognize that it should use that?
It sounds like you are asking how you can achieve this ... but I can't figure out why? If you don't want it to be controlled ScreenManager
, perhaps? However, the only way you can have KanjiOriginScreen
a kv file without <>
is inside another root widget. For example, see Testy
and ScreenTwo
as they are in the kv file under <Manager>
in the answer to your other question ( here ). They do not <>
, because they are instances of a class, WITHIN another class (Manager class). Only root widgets have <>
them around them in the kv file. If none of this makes sense to you, then you need to make a kiwi tutorial.
Check out this tutorial I did a while ago, it explains a bit about root widgets in kv (around 4.30).
source to share