Python code inside kv file

26 views
Skip to first unread message

Karam Jaber

unread,
Aug 2, 2018, 9:55:19 AM8/2/18
to Kivy users support
I've read kv documentation and found out that it is possible to use python code inside kv files.
If for example i have a file that has global parameters:

global_param.py
MY_GLOB_VAR="abc"

and i have the following kv file

my_kv.kv

#: import global_param global_param
my_box
:
 id
:
global_param.MY_GLOB_VAR

main.py
import global_param

class my_box(Boxlayout):
   
pass

class mainApp(app):
   
def build():
       
return my_box()
 
def my_func(self):
       
print(self.my_box.ids[global_param.MY_GLOB_VAR])

but in my_func it prints the string "global_param.MY_GLOB_VAR" instead of "abc" as i intended.
How i can make the term global_param.MY_GLOB_VAR to be evaluated as python code and not as string ?
Message has been deleted

OptimusGREEN

unread,
Aug 2, 2018, 3:04:55 PM8/2/18
to Kivy users support
Try

#: import MY_GLOB_VAR global_param.MY_GLOB_VAR

Karam Jaber

unread,
Aug 5, 2018, 3:55:29 AM8/5/18
to Kivy users support
That doesn't seems to make any sense.
The idea is using the global_param as container to all the ids in the kv file and importing them 1 by one is not something that i want to do

On Thursday, 2 August 2018 22:04:55 UTC+3, OptimusGREEN wrote:
Try

#: import MY_GLOB_VAR global_param.MY_GLOB_VAR

Reply all
Reply to author
Forward
0 new messages