【问题标题】:Kivy crashes. Don't know why基维崩溃。不知道为什么
【发布时间】:2021-12-02 12:35:24
【问题描述】:

首先我知道这并不具体,但现在我无法弄清楚为什么,所以我会在有原因时编辑问题。对不起!。

我从 Kivy 开始,我有很多问题,但在这种情况下,我什至没有错误消息!

这是代码,这只是对 Pong Game 教程的改编,但只使用了一个 .py 文件(没有 .kv 文件):

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
from random import randint
from kivy.graphics import Color, Ellipse, Line, Rectangle
from kivy.core.window import Window
from kivy.uix.label import Label
from kivy.graphics.instructions import InstructionGroup
from kivy.uix.gridlayout import GridLayout

class PongBall(Widget):
    def __init__(self):
        self.id = "ball"
        velocity_x = NumericProperty(0)
        velocity_y = NumericProperty(0)
        velocity = ReferenceListProperty(velocity_x, velocity_y)
        self.canvas = Ellipse(pos=(Window.width*0.5, Window.height*0.5), size=(50,50), Color=(1.0, 1.0, 0.5))
        #self.canvas.add(Ellipse(pos=(width*0.5, height*0.5), size=(50,50), Color=(1.0, 1.0, 0.5)))#This way doesn't works

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

    def serve_ball(self):
        self.center = self.center
        self.velocity = Vector(4, 0).rotate(randint(0, 360))
        print "ball served"


class PongGame(Widget):
    def __init__(self):
        mainLayout = GridLayout(cols=1)
        ball = PongBall()
        ball.id = "pong_ball"
        ball.center = Window.center
#        mainLayout.add_widget(Rectangle(pos=(Window.width*0.5, 0), size=(10, Window.height)))#Unresearched error.
        mainLayout.add_widget(Label(id="playerOneScore", font_size=70, center_x = Window.width*0.25, top=Window.height-50, text="0"))
        mainLayout.add_widget(Label(id="playerTwoScore", font_size=70, center_x = Window.width*0.75, top=Window.height-50, text="0"))
        mainLayout.add_widget(ball)
        print "Hello"
        ball.serve_ball()

    def update(self, dt):
        print "updated!"
        ball.move()

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

        # bounce off left and right
        if (self.ball.x < 0) or (self.ball.right > self.width):
            self.ball.velocity_x *= -1


class PongApp(App):
    def build(self):
        game = PongGame()
        print "game created!"
        Clock.schedule_interval(game.update, 1.0 / 60.0)#Clock statement neverminds for the error.
        return game


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

有 Kivy 经验的人能够理解为什么 Kivy 会在这段代码中崩溃?

我正在使用 python 2.7.9 和 Kivy 1.10.1

【问题讨论】:

  • 为什么没有错误信息?运行此代码时,控制台中会打印什么?
  • 没有错误!。它只是崩溃而没有错误! os提示:pythonw.exe停止工作(我正在使用python idle)
  • @Reaversword 从 CMD 执行你的代码,你肯定会得到错误信息,另一方面 IDLE 有很多限制,尤其是在 GUI 中
  • 与来自 cmd 或 powershell 的“python main.py”的结果相同。一点头绪都没有。
  • 所以当你写python main.py 时,你真的没有输出吗?就好像你没有输入任何内容就按了 Enter 键一样?

标签: kivy


【解决方案1】:

什么?不要在以下位置添加斜线: from kivy.properties import NumericProperty, ReferenceListProperty,\ ObjectProperty 您不需要为它换行,也不要在对象属性附近使用标签。

【讨论】:

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