不需要的重复Kivy小部件

huangapple go评论47阅读模式
英文:

Unwanted duplicate kivy widget

问题

只有一个球状小部件在屏幕上弹跳显示。但是实际上有两个球状小部件被显示,一个在移动,另一个固定在屏幕中央。以下是我的Python代码:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty, NumericProperty, ReferenceListProperty
from kivy.lang.builder import Builder
from kivy.vector import Vector
from kivy.clock import Clock

刷新速率 = 1/60

Builder.load_file("./balls.kv")

class Balls(Widget):
    vx = NumericProperty(0)
    vy = NumericProperty(0)
    velocity = ReferenceListProperty(vx, vy)

    def move(self):
        self.pos = Vector(self.velocity) + self.pos

class BallsLogic(Widget):
    ball = ObjectProperty(None)

    def start(self):
        self.ball.velocity = 10, -3

    def update(self, dt):
        self.ball.move()
        if self.ball.y < self.y or self.ball.top > self.top:
            self.ball.vy *= -1
        if self.ball.x < self.x or self.ball.right > self.right:
            self.ball.vx *= -1

class BallsApp(App):
    def build(self):
        bl = BallsLogic()
        bl.start()
        Clock.schedule_interval(bl.update, 刷新速率)
        return bl

if __name__ == "__main__":
    BallsApp().run()

伴随的kv文件如下:

#kivy 1.0.9

<Balls>
    size: 50, 50
    canvas:
        Ellipse:
            size: self.size
            pos: self.pos

<BallsLogic>
    ball: ball
    Balls:
        center: self.parent.center
        id: ball

我尝试重新编写程序以查找可能出错的地方。我发现问题在于实现move方法时。一个球状小部件保持不动,而另一个球则快速穿越屏幕。我只想要一个球状小部件在屏幕上弹跳,但我不知道为什么会出现固定的球状小部件,也不知道为什么它不受updatemove方法的影响。

英文:

I only want one ball widget displayed bouncing around on the screen. However two ball widget's are displayed, one moving and the other stationary positioned in the center of the screen. This is my python code:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty, NumericProperty, ReferenceListProperty
from kivy.lang.builder import Builder
from kivy.vector import Vector
from kivy.clock import Clock


refresh_rate = 1/60

Builder.load_file(&quot;./balls.kv&quot;)


class Balls(Widget):

    vx = NumericProperty(0)
    vy = NumericProperty(0)

    velocity = ReferenceListProperty(vx,vy)

    def move(self):
        self.pos = Vector(self.velocity) + self.pos


class BallsLogic(Widget):

    ball = ObjectProperty(None)

    def start(self):
        self.ball.velocity = 10, -3

    def update(self, dt):

        self.ball.move()

        if self.ball.y &lt; self.y or self.ball.top &gt; self.top:
            self.ball.vy *= -1

        if self.ball.x &lt; self.x or self.ball.right &gt; self.right:
            self.ball.vx *= -1


class BallsApp(App):

    def build(self):
        bl = BallsLogic()
        bl.start()
        Clock.schedule_interval(bl.update, refresh_rate)
        return bl


if __name__ == &quot;__main__&quot;:
    BallsApp().run()

With the accompanying kv file:

#kivy 1.0.9

&lt;Balls&gt;
    size:50,50
    canvas:
        Ellipse:
            size: self.size
            pos: self.pos

&lt;BallsLogic&gt;
    ball: ball
    Balls:
        center: self.parent.center
        id: ball
        

I've tried re-writing the program to see where I may have gone wrong. I notice the issue when I implement the move method. One ball widget stay's in place, and the other zooms across the screen. I only want one ball widget bouncing around, and I can't figure out where the stationary one is coming from or why it isn't effected by the update or move methods.

答案1

得分: 2

Kivy会自动加载一个命名正确的 kv 文件,请参阅文档。在你的情况下,名为 balls.kvkv 文件将会自动加载。此外,下面的代码:

Builder.load_file("./balls.kv")

加载了相同的文件。这会导致 balls.kv 文件被加载两次,同时会向 BallsLogic 组件添加两个 Balls 组件。修复方法是简单地删除上面的代码行,以便 balls.kv 文件只加载一次。

英文:

Kivy will automatically load a properly named kv file, see the documentation. In your case, a kv file with the name balls.kv will automatically be loaded. Also, the code:

Builder.load_file(&quot;./balls.kv&quot;)

loads the same file. This results in the balls.kv file being loaded twice, and two Balls widgets being added to the BallsLogic widget. The fix is to simply remove the above line of code so that the balls.kv file only gets loaded once.

huangapple
  • 本文由 发表于 2023年2月24日 03:36:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549539.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定