Skip to main content
 首页 » 编程设计

python之标签颜色改变其他标签

2024年11月01日11kuangbin

我更改了保持乒乓球比赛得分的小部件的颜色,但是一旦我点击开始玩游戏,球的颜色和 Racket 的颜色也会发生变化。球和 Racket 一开始是白色的,我想让它们保持白色。

#:kivy 1.8.0 
 
 
<PongBall>: 
    size: 30,30 
    canvas: 
        Ellipse: 
            pos: self.pos 
            size: self.size 
 
<PongPaddle>: 
    size: 100, 15 
    canvas: 
        Rectangle: 
            pos: self.pos 
            size: self.size 
 
 
<PongGame>: 
    ball: pong_ball 
    player1: player_bottom 
    test_text: test_text 
 
    canvas: 
        Color:  
            rgb: (1, 1, 1) 
        Rectangle: 
            pos: 0, self.height-30 
            size: self.width, 30 
 
    Label: 
        font_size: 30 
        center_x: 30 
        top: root.top 
        text: str(root.player1.score) 
        color: 1,0,0,1 
 
    Label: 
        id: test_text 
        font_size: 60 
        center_x: root.width/2 
        top:  root.top-(root.top - (root.top/2))/2 
        text: 'Tap to Play!' 
 
    PongBall: 
        id: pong_ball 
        center: self.parent.center 
 
    PongPaddle: 
        id: player_bottom 
        x: root.center_x 
        center_y: root.y+self.height 

请您参考如下方法:

Label: 
    font_size: 30 
    center_x: 30 
    top: root.top 
    text: str(root.player1.score) 
    color: 1,0,0,1 

将两个标签代码移动到你的 kv 文件的底部,它应该只会改变分数字符串的颜色。

<PongBall>: 
    size: 50, 50  
    canvas: 
        Ellipse: 
            pos: self.pos 
            size: self.size           
 
<PongPaddle>: 
    size: 25, 200 
    canvas: 
        Rectangle: 
            pos:self.pos 
            size:self.size 
<PongGame>: 
    ball: pong_ball 
    player1: player_left 
    player2: player_right 
    canvas: 
        Color: 
            rgb: (1, 1, 1) 
        Rectangle: 
            pos: self.center_x-5, 0 
            size: 20, self.height 
 
 
    PongBall: 
        id: pong_ball 
        canvas.before: 
            Color: 
                rgb: (1, 0, 0) #red ball 
        center: self.parent.center 
 
    PongPaddle: 
        id: player_left 
        x: root.x 
        canvas.before: 
            Color: 
                rgb: 1, 1, 0 # yellow paddle 
        center_y: root.center_y 
 
    PongPaddle: 
        id: player_right 
        x: root.width-self.width 
        canvas.before: 
            Color: 
                rgb: 0, 1, 0 #green paddle 
        center_y: root.center_y 
 
    Label: 
        font_size: 70 
        center_x: root.width / 4 
        top: root.top - 50 
        text: str(root.player1.score) 
        color: 0,0,1,1 
 
    Label: 
        font_size: 70 
        center_x: root.width * 3 / 4 
        top: root.top - 50 
        text: str(root.player2.score) 
        color: 0,0,1,1