Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将对象属性传递给Kivy.kv文件_Python_Kivy - Fatal编程技术网

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

Python 如何将对象属性传递给Kivy.kv文件,python,kivy,Python,Kivy,我的queston与Python/Kivy有关。 我需要根据游戏块的类型使用不同的纹理(即正方形区域,40 x 40)。我尝试了最自然的方法,即将类型定义为对象属性,并测试.kv文件中的值。唉,该属性无法识别。 我很确定这很简单,是因为我对一些概念的误解。从现有的文件中我还是看不出来。 提前感谢你给我指明了正确的方向 考虑下面的示例代码。这是相当大的一块,但这是完成运行的成本 # Imports from os import system as _system from kivy.app im

我的queston与Python/Kivy有关。 我需要根据游戏块的类型使用不同的纹理(即正方形区域,40 x 40)。我尝试了最自然的方法,即将类型定义为对象属性,并测试.kv文件中的值。唉,该属性无法识别。 我很确定这很简单,是因为我对一些概念的误解。从现有的文件中我还是看不出来。 提前感谢你给我指明了正确的方向

考虑下面的示例代码。这是相当大的一块,但这是完成运行的成本

# 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     
随附的示例.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

您在kv语言中未正确使用Python表达式。有关正确的语法,请参见。具体来说,您需要将if表达式移动到
源代码:

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

非常感谢。现在它告诉文件“C:\Moje\Example\Example.kv”,第7行“Textures/Dirt.png”if self.typ==2^ SyntaxError:解析时出现意外的EOF在哪里可以找到语法引用?我尝试过,但没有成功。请使用“root.typ”而不是“self.typ”,因为您引用的是“Block”的属性。答案已更新,谢谢。但它仍然是“解析时意外的EOF”。我用记事本仔细地编辑了.kv文件,以确保没有隐藏的字符,因此我认为这是一个相当大的解析问题。我在哪里可以找到推荐人?
Rectangle:
    source: "Textures/Dirt.png" if root.typ == 2
    pos: self.pos
    size: 40, 40