簡體   English   中英

如何將變量從.py文件帶到.kv文件?

[英]How to bring a variable from the .py file to the .kv file?

Kv語言可以使用以下語法從其他文件導入名稱:

#:import name x.y.z

# The same as `from x.y import z as name` in Python code

但是沒有提到如何從使用該kv語言的同一模塊中導入值。
假設我有以下代碼:

from kivy.app import App
from kivy.lang import Builder
from kivy.atlas import Atlas
from kivy.uix.button import Button

theme = Atlas('path/to/atlas')

Builder.load_string('''
<MyWidget>:
    background_normal: theme['widget_bg']
''')

class MyWidget(Button):
    pass

class TestApp(App):
    def build(self):
       return MyWidget() 

TestApp().run()

我想將theme Atlas對象導入kv代碼以設置適當的背景。 怎么辦呢?

您可以將當前模塊稱為__main__

#:import theme __main__.theme

<MyWidget>:
   background_normal: theme['widget_bg']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM