简体   繁体   English

Kivy:如何使用数字属性更改窗口小部件大小?

[英]Kivy: how to change widget size using numericproperty?

Trying to make a brick widget change size each time it respawns. 尝试使砖小部件每次重生时都更改其大小。

The py file has py文件有

class Game(FloatLayout):
    player = ObjectProperty(None)
    playbutton = ObjectProperty(None)
    ratebutton = ObjectProperty(None)
    brickg = ObjectProperty(None)
    ballsin = NumericProperty(0)
    bricklist = ListProperty([])
    score = NumericProperty(0)
    switch = NumericProperty(0)
    level = NumericProperty(0)

     def __init__(self, *args, **kwargs):
        super(Game, self).__init__(*args, **kwargs)
        Clock.schedule_interval(self.update, 1./60)

    def spawn_brick(self, *args):
        b2 = BrickGreen(x=randint(50, self.width - 50), \
        y=randint(self.height - self.height / 4, self.height - (self.height/13)))
        self.bricklist.append(b2)
        self.add_widget(b2)

    def check_brick_spawn(self, *args):

        if len(self.bricklist) == 0:
            if self.level == 0:
                BrickGreen.brickwidth = 100
                self.spawn_brick()
            elif self.level == 1:
                BrickGreen.brickwidth = 75
                self.spawn_brick()
            else:
                BrickGreen.brickwidth = 50
                self.spawn_brick()

class BrickGreen(Widget):
    def __init__(self, **kwargs):
        super(BrickGreen, self).__init__(**kwargs)

        brickwidth = NumericProperty(0)

and the kv file has 并且kv文件具有

<BrickGreen>:
    size_hint: None, None
    size: self.brickwidth, 25
    canvas:
        Color:
            rgba: 0, 1, 0, 1
        Rectangle:
            pos: self.pos
            size: self.size

Essentially, level 0 should have bricks with length of 100, once it breaks it goes to level 1 and should spawn bricks with lenght of 75, but it's staying at 100 从本质上讲,级别0应该具有长度为100的砖块,一旦断开,它就会到达级别1,并且应该生成长度为75的砖块,但是它保持在100

BrickGreen.brickwidth = 100 BrickGreen.brickwidth = 100

This replaces the NumericProperty at class level with the number 100. You need to set the value of brickwidth for instances of your objects, not the classes themselves. 这将用数字100替换类级别的NumericProperty。您需要为对象的实例而不是类本身的实例设置brickwidth的值。 In this case, you could (for instance) pass the width you want to your spawn_brick method, and use it when instantiating the BrickGreen. 在这种情况下,您可以(例如)将所需的宽度传递给spawn_brick方法,并在实例化BrickGreen时使用它。

You also need to declare the NumericProperty at class level, not inside __init__ . 您还需要在类级别而不是__init__内声明NumericProperty。

I suggest reading the kivy documentation on properties and going through the examples to see how these should be used. 我建议阅读有关属性的kivy文档,并仔细阅读示例以了解应如何使用这些属性。

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

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