【问题标题】:kivy remove_widget not removingkivy remove_widget 不删除
【发布时间】:2016-01-06 22:07:22
【问题描述】:

我正在学习 kivy,并且正在玩 PONG 示例。我添加了一个在玩家达到给定分数后出现的按钮。

    if self.player2.score ==2 or self.player1.score==2:
        self.serve_ball(vel=(0, 0))
        self.btn= Button()
        self.add_widget(self.btn)
        self.btn.bind(on_press=self.callback)

它绑定到重置分数和消息的函数回调,但我尝试了几种方法来在按下后删除按钮但没有成功。

def callback(self, instance):
    self.player2.score=0
    self.player1.score=0
    self.message1=""
    self.message2=""
    self.serve_ball(vel=(4,0))
    self.remove_widget(self.btn)

有什么想法吗?谢谢,这是完整的代码

from kivy.app import App 
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.properties import StringProperty, NumericProperty, ReferenceListProperty, ObjectProperty
from kivy.vector import Vector
from kivy.clock import Clock
from random import randint


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):

    #Vel of ball in x & y axis
    velocity_x = NumericProperty(0)
    velocity_y = NumericProperty(0)

    #referencia para usar como vector
    velocity = ReferenceListProperty(velocity_x,velocity_y)

    #funcion move  1 paso en intervalos para animar la bola
    def move(self):
        self.pos = Vector(*self.velocity) + self.pos


class PongGame(Widget):
    ball = ObjectProperty(None)
    player1 = ObjectProperty(None)
    player2 = ObjectProperty(None) 
    message2 = StringProperty("")
    message1 = StringProperty("")



    def callback(self, instance):
        self.player2.score=0
        self.player1.score=0
        self.message1=""
        self.message2=""
        self.serve_ball(vel=(4,0))
        self.remove_widget(self.btn)

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

    def btnn(self, instance):
        self.remove_widget(self.btn)

    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))

        #emilio code
        if self.player2.score ==2 or self.player1.score==2:
            self.serve_ball(vel=(0, 0))
            self.btn= Button()
            self.add_widget(self.btn)
            self.btn.bind(on_press=self.callback)

            if self.player2.score == 2:
                self.message2= "player2 won!"


            if self.player1.score == 2:
                self.message1= "player1 won!"


    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()`

这是kv文件以防万一:

#:kivy 1.0.9


<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:
        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)
        color: (255,0,255,1)
    Label:
        font_size: 70
        center_x: root.width * 3 / 4
        top: root.top - 50
        text: str(root.player2.score)
        color: (255,0,255,1)
    Label:
        font_size: 40
        center_x: root.width * 3/4
        top: root.center_y
        text: str(root.message2)
        color: (0,255,0,1)
    Label:
        font_size: 40
        center_x: root.width / 4
        top: root.center_y
        text: str(root.message1)
        color: (0,255,0,1)

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


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


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

【问题讨论】:

    标签: button kivy


    【解决方案1】:

    更新函数被调用了很多次,每次满足你的条件时,你都会创建另一个按钮。仅删除单击它时创建的最后一个按钮...

    这是你的错误:

    if self.player2.score ==2 or self.player1.score==2:
        print 'WOO' # see how many times this is gonna be printed
        #make sure you don't get here more than once per game :)
        self.serve_ball(vel=(0, 0))
        self.btn= Button()
        self.add_widget(self.btn)
        self.btn.bind(on_press=self.callback)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多