简体   繁体   English

如何将对象属性传递给 Kivy .kv 文件

[英]How can I pass object properties to the Kivy .kv file

My queston is related to Python / Kivy.我的问题与 Python / Kivy 有关。 I'm in need to use various textures for game blocks (ie square fields, 40 x 40) depending on their types.我需要根据它们的类型为游戏块(即方形字段,40 x 40)使用各种纹理。 I tried the most natural way to me, ie define the type as an object attribute, and test the value in the .kv file.我尝试了对我来说最自然的方法,即将类型定义为对象属性,并测试 .kv 文件中的值。 Alas, the attribute is not recognized.唉,该属性无法识别。 I'm pretty sure this is something simple, and resulting from my misunderstanding of some concept.我很确定这很简单,并且是由于我对某些概念的误解造成的。 Stil I can't make it out from the available docs.仍然我无法从可用的文档中弄清楚。 Thanks in advance for putting me in the right direction.提前感谢您让我朝着正确的方向前进。

Consider the following example code.考虑以下示例代码。 It's quite a big chunk, but this is the cost of it being complete to run.这是相当大的一块,但这是完成运行的成本。

# Imports
from os import system as _system

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Color
from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty
from kivy.vector import Vector
from kivy.config import Config
from kivy.uix.image import Image

# Size of the map in memory
MAXX = 1001
MAXY = 150

# Size of the display, counted in blocks
MAXSCREENX = 91
MAXSCREENY = 60

# Single block size
BLOCK_SIZE = 40

# Global data structures
game_state = []
mapa = []
player_position = []
equipment = []
eye_direction = 0

# Various subclasees, because of different GIFs defined in the .ky file
class Block(Widget):
    typ = NumericProperty(0)

class GameWidget(Widget):
    xshift = NumericProperty(0)
    yshift = NumericProperty(0)

class ExampleApp(App):
    def build(self):
        game = GameWidget()
        game.xshift = 0
        game.yshift = 0
        game.screen_map = []
        for i in range(MAXSCREENX):
            for j in range(MAXSCREENY):
                idx = i*MAXSCREENY + j
                game.add_widget(Block(), idx)
                game.children[idx].pos = (i * BLOCK_SIZE, j * BLOCK_SIZE)
                game.children[idx].typ = 2
        return game

if __name__ == '__main__':
    # Set the window size
    Config.set('graphics', 'width', str(MAXSCREENX * BLOCK_SIZE))
    Config.set('graphics', 'height', str(MAXSCREENY * BLOCK_SIZE))

    ExampleApp().run() # Launch the main application     

with the accompanying example.kv file:随附的 example.kv 文件:

#:kivy 1.9.0

<Block>:
    size: 40, 40
    canvas:
        Rectangle:
            if self.typ == 2:
                source: "Textures/Dirt.png"
            pos: self.pos
            size: 40, 40

Alas, I get the following error:唉,我收到以下错误:

kivy.lang.ParserException: Parser: File "C:\Moje\Example\example.kv", line 7:
...
       5:    canvas:
       6:        Rectangle:
 >>    7:            if self.typ == 2:
       8:                source: "Textures/Dirt.png"
       9:            pos: self.pos

You are improperly using Python expressions in kv language.您在 kv 语言中错误地使用了 Python 表达式。 See the docs for proper syntax.有关正确的语法,请参阅文档 Specifically, you need to move your if expression after source:具体来说,您需要在source:之后移动 if 表达式source:

Rectangle:
    source: "Textures/Dirt.png" if root.typ == 2
    pos: self.pos
    size: 40, 40

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM