2016-07-29 28 views
0

我是新來的Python GUI,想試試Kivy。 但已經在傍教程中,我卡住: 我運行原始腳本,並得到了以下錯誤:Kivy教程 - pong.py沒有聽衆錯誤 - .kv沒有正確關閉

[WARNING   ] [Lang  ] The file C:\Users\Canopy\Skripts\Pong\pong.kv is loaded multiples times, you might have unwanted behaviors. 
[INFO    ] [Base  ] Start application main loop 
[ERROR    ] [Base  ] No event listeners have been created 
[ERROR    ] [Base  ] Application will leave 

爲什麼這些listerners失蹤,爲什麼其他(顯然)運行的代碼?

編輯:重新啓動固定它一次,但下一次我運行該應用程序我 遇到了同樣的問題。 .kv似乎沒有正確關閉。

我在Win 7 這些運行的Python 2.7(樹冠)的腳本: main.py:

from kivy.app import App 
from kivy.uix.widget import Widget 
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 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)) 

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

和Pong.py:

#:kivy 1.9.1 

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

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

    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 

回答

0

確定,基本的IT解決方案實際上工作: 重新聲明PC後,錯誤不再發生。 編輯:再次運行應用程序後,我遇到了同樣的問題。仍需要解決方案!