我有可能从kivi语言中的一个窗口小部件引用另一个窗口小部件的属性吗?

时间:2019-06-21 22:59:21

标签: python kivy kivy-language

我在Pong教程结尾的教程中先从猕猴桃玩起,然后根据得分添加'Winner'标签。

为此,我设计了一个获奖者标签:

<Winner>:
    Label:
        font_size: 200
        #center_x: self.parent.width * .5
        #center_y: self.parent.top - .5
        text: "wINNER!"

根据Pong游戏的尺寸,我想将此标签放在中间,但最终会在左下方。

我也尝试过直接在python中添加小部件(请参见更新方法),但是我仍然不知道如何动态地进行放置,就像其他带有分数的标签如何在kivy文件中的PongGame中放置一样

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.properties import (
    NumericProperty, ReferenceListProperty, ObjectProperty
)
from kivy.vector import Vector
from kivy.clock import Clock


class PongPaddle(Widget):
    score = NumericProperty(0)

    def bounce_ball(self, ball):
        if self.collide_widget(ball):
            vx, vy = ball.velocity
            offset = (ball.center_y - self.center_y) / (self.height / 2)
            bounced = Vector(-1 * vx, vy)
            vel = bounced * 1.1
            ball.velocity = vel.x, vel.y + offset


class PongBall(Widget):
    velocity_x = NumericProperty(0)
    velocity_y = NumericProperty(0)
    velocity = ReferenceListProperty(velocity_x, velocity_y)

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

class Winner(Widget):
    pass



class PongGame(Widget):
    ball = ObjectProperty(None)
    player1 = ObjectProperty(None)
    player2 = ObjectProperty(None)

    def serve_ball(self, vel=(4, 0)):
        self.ball.center = self.center
        self.ball.velocity = vel

    def update(self, dt):
        self.ball.move()

        # bounce of paddles
        self.player1.bounce_ball(self.ball)
        self.player2.bounce_ball(self.ball)

        # bounce ball off bottom or top
        if (self.ball.y < self.y) or (self.ball.top > self.top):
            self.ball.velocity_y *= -1

        # went of to a side to score point?
        if self.ball.x < self.x:
            self.player2.score += 1
            self.serve_ball(vel=(4, 0))
        if self.ball.x > self.width:
            self.player1.score += 1
            self.serve_ball(vel=(-4, 0))
        if self.player1.score>0:
            Clock.unschedule(self.update)
            self.add_widget(Winner())
            # self.add_widget(text='bla',center_y=self.parent.top-50)
            self.add_widget(Label(text='bla',center_y=self.parent.top-50))
        if self.player2.score>0:
            Clock.unschedule(self.update)
            self.add_widget(Winner())
            self.add_widget(Label(text='bla',center_y=self.parent.top-50))
    def on_touch_move(self, touch):
        if touch.x < self.width / 3:
            self.player1.center_y = touch.y
        if touch.x > self.width - self.width / 3:
            self.player2.center_y = touch.y


class PongApp(App):
    def build(self):
        game = PongGame()
        game.serve_ball()
        Clock.schedule_interval(game.update, 1.0 / 60.0)
        return game


if __name__ == '__main__':
    PongApp().run()

猕猴桃

#:kivy 1.0.9

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

<Winner>:
    Label:
        font_size: 200
        center_x: self.parent.width * .5
        center_y: self.parent.top - .5
        text: "wINNER!"

<PongPaddle>:
    size: 20, 200
    canvas:
        Rectangle:
            pos: self.pos
            size: self.size

<PongGame>:
    ball: pong_ball
    player1: player_left
    player2: player_right

    canvas:
        Rectangle:
            pos: self.center_x - 5, 0
            size: 10, self.height

    Label:
        font_size: 70
        center_x: root.width / 4
        top: root.top - 50
        text: str(root.player1.score)

    Label:
        font_size: 70
        center_x: root.width * 3 / 4
        top: root.top - 50
        text: str(root.player2.score)

    Label:
        font_size: 70
        center_x: root.width * .5
        top: root.top - 50
        text: "Winner!"

    PongBall:
        id: pong_ball
        center: self.parent.center

    PongPaddle:
        id: player_left
        x: root.x
        y: root.center_y

    PongPaddle:
        id: player_right
        x: root.width-self.width
        center_y: root.center_y

如何引用父窗口小部件的属性? 谢谢!

1 个答案:

答案 0 :(得分:1)

kv文件和Python脚本需要以下增强功能才能解决该问题。

kv文件

  • 使用app.root来指代根窗口小部件。

Kv language » Rule context

  

Kv语言有三个关键字:

     
      
  • app :始终引用您的应用程序实例。
  •   
  • root :指当前规则中的基本小部件/模板
  •   
  • 自我:始终引用当前小部件
  •   

Kivy Language » Value Expressions, on_property Expressions, ids, and Reserved Keywords

  

自己

     

关键字self引用“当前小部件实例”:

Button:
    text: 'My state is %s' % self.state
     

root

     

此关键字仅在规则定义中可用,代表   规则的根窗口小部件(规则的第一个实例):

<MyWidget>:
    custom: 'Hello world'
    Button:
        text: root.custom
     

应用

     

此关键字始终引用您的应用实例。相当于一个   在Python中调用kivy.app.App.get_running_app()

Label:
    text: app.name

摘要-kv文件

<Winner>:
    font_size: 200
    center: app.root.center
    text: "WINNER!"

py文件

  • 将类Winner()的继承从Widget更改为Label,因为我们实际上不需要两个小部件,这样可以减少资源使用,例如内存,应用程序大小等。
  • 添加Winner对象。

代码段-py文件

class Winner(Label):
    pass


class PongGame(Widget):
    ...
    def update(self, dt):
        self.ball.move()
        ...
        if self.player1.score > 0:
            self.add_widget(Winner())
        if self.player2.score > 0:
            self.add_widget(Winner())

    def on_touch_move(self, touch):
        ...

输出

Result

相关问题