【问题标题】:Python returns an error when i try to quit the application window当我尝试退出应用程序窗口时,Python 返回错误
【发布时间】:2019-12-20 16:58:32
【问题描述】:

我正在关注这个小项目的视频,但遇到了这个错误

Traceback (most recent call last):
File "C:/Users/Dell/Desktop/Pygame/games/pong.py", line 92, in <module>
ball.setx(ball.xcor()+ball.dx)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python37-32\lib\turtle.py", line 1808, in setx
self._goto(Vec2D(x, self._position[1]))
File "C:\Users\Dell\AppData\Local\Programs\Python\Python37-32\lib\turtle.py", line 3158, in _goto
screen._pointlist(self.currentLineItem),
File "C:\Users\Dell\AppData\Local\Programs\Python\Python37-32\lib\turtle.py", line 755, in _pointlist
cl = self.cv.coords(item)
File "<string>", line 1, in coords
File "C:\Users\Dell\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2469, in coords
self.tk.call((self._w, 'coords') + args))]
_tkinter.TclError: invalid command name ".!canvas"

当我按下退出按钮关闭窗口时会发生这种情况。 如何解决?

代码如下:

import turtle

wn = turtle.Screen()
wn.title("Pong")
wn.bgcolor('black')
wn.setup(width=800, height=600)
wn.tracer(0)
#Score
score_a = 0
score_b = 0


#paddle A
paddle_a = turtle.Turtle()
paddle_a.speed(0)
paddle_a.shape("square")
paddle_a.color("white")
paddle_a.shapesize(stretch_wid=5, stretch_len = 1)
paddle_a.penup()
paddle_a.goto(-350,0)
#paddle B
paddle_b = turtle.Turtle()
paddle_b.speed(0)
paddle_b.shape("square")
paddle_b.color("white")
paddle_b.shapesize(stretch_wid=5, stretch_len = 1)
paddle_b.penup()
paddle_b.goto(350,0)


#Ball

ball = turtle.Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("white")
ball.penup()
ball.goto(0,0)
ball.dx = 0.5
ball.dy = 0.5

#pen

pen = turtle.Turtle()
pen.speed(0)
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0,260)
pen.write("Player A: 0 Player B: 0", align="center", font=('Courier', 24, "normal"))

#Function
def paddle_a_up():
    y = paddle_a.ycor()
    y+=20
    paddle_a.sety(y)

def paddle_a_down():
    y = paddle_a.ycor()
    y-=20
    paddle_a.sety(y)

def paddle_b_up():
    y = paddle_b.ycor()
    y+=20
    paddle_b.sety(y)


def paddle_b_down():
    y = paddle_b.ycor()
    y-=20
    paddle_b.sety(y)
#Keyboard binding
wn.listen()
wn.onkeypress(paddle_a_up, "w")
wn.onkeypress(paddle_a_down, "s")
wn.onkeypress(paddle_b_up, "Up")
wn.onkeypress(paddle_b_down, "Down")

running = True
#main game loop
while running:
    wn.update()

    #move the ball
    ball.setx(ball.xcor()+ball.dx)
    ball.sety(ball.ycor()+ball.dy)

    #border
    if ball.ycor() > 280:
        ball.sety(280)
        ball.dy*=-1

    if ball.ycor() < -280:
        ball.sety(-280)
        ball.dy*=-1

    if ball.xcor() > 380:
        ball.goto(0,0)
        ball.dx*=-1
        score_a+=1
        pen.clear()
        pen.write(f"Player A: {score_a} Player B: {score_b}", align="center", font=('Courier', 24, "normal"))

    if ball.xcor() < -380:
        ball.goto(0,0)
        ball.dx*=-1
        score_b += 1
        pen.clear()
        pen.write(f"Player A: {score_a} Player B: {score_b}", align="center", font=('Courier', 24, "normal"))

    #colliosion
    if (ball.xcor() > 330 and ball.xcor() < 340) and (ball.ycor() < paddle_b.ycor() + 50 and ball.ycor() > paddle_b.ycor() -50):
        ball.setx(330)
        ball.dx*=-1
    if (ball.xcor() < -330 and ball.xcor() > -340) and (ball.ycor() < paddle_a.ycor() + 50 and ball.ycor() > paddle_a.ycor() -50):
        ball.setx(-330)
        ball.dx*=-1


我阅读了有关放置 .mainloop() 方法的信息,但我不知道该放在哪里。我非常了解while循环永远运行并且没有条件在这里停止所以我如何使循环停止?按下退出按钮后如何告诉python停止循环并退出窗口?

【问题讨论】:

    标签: python python-3.x turtle-graphics


    【解决方案1】:

    看起来turtle 使用tkinter。在 Tk 中,有一个名为 protocol 的方法,您可以使用它来控制某些事件发生时会发生什么,其中一个事件正在点击红色的“X”。

    为了调用它,我们需要获取根级窗口。

    放置

    canvas = wn.getcanvas()
    root = canvas.winfo_toplevel()
    

    将 root 设置为您的根窗口。

    您可以从这里使用root.protocol("WM_DELETE_WINDOW", on_close) 调用名为on_close 的函数或任何您想命名的函数。

    您的函数可能看起来像这样。

    def on_close():
        global running
        running = False
    

    这会让你跳出循环并关闭你的程序。

    完整代码。

    import turtle
    
    wn = turtle.Screen()
    
    canvas = wn.getcanvas()
    root = canvas.winfo_toplevel()
    
    wn.title("Pong")
    wn.bgcolor('black')
    wn.setup(width=800, height=600)
    wn.tracer(0)
    #Score
    score_a = 0
    score_b = 0
    
    
    #paddle A
    paddle_a = turtle.Turtle()
    paddle_a.speed(0)
    paddle_a.shape("square")
    paddle_a.color("white")
    paddle_a.shapesize(stretch_wid=5, stretch_len = 1)
    paddle_a.penup()
    paddle_a.goto(-350,0)
    #paddle B
    paddle_b = turtle.Turtle()
    paddle_b.speed(0)
    paddle_b.shape("square")
    paddle_b.color("white")
    paddle_b.shapesize(stretch_wid=5, stretch_len = 1)
    paddle_b.penup()
    paddle_b.goto(350,0)
    
    
    #Ball
    
    ball = turtle.Turtle()
    ball.speed(0)
    ball.shape("circle")
    ball.color("white")
    ball.penup()
    ball.goto(0,0)
    ball.dx = 0.5
    ball.dy = 0.5
    
    #pen
    
    pen = turtle.Turtle()
    pen.speed(0)
    pen.color("white")
    pen.penup()
    pen.hideturtle()
    pen.goto(0,260)
    pen.write("Player A: 0 Player B: 0", align="center", font=('Courier', 24, "normal"))
    
    #Function
    def paddle_a_up():
        y = paddle_a.ycor()
        y+=20
        paddle_a.sety(y)
    
    def paddle_a_down():
        y = paddle_a.ycor()
        y-=20
        paddle_a.sety(y)
    
    def paddle_b_up():
        y = paddle_b.ycor()
        y+=20
        paddle_b.sety(y)
    
    
    def paddle_b_down():
        y = paddle_b.ycor()
        y-=20
        paddle_b.sety(y)
    #Keyboard binding
    wn.listen()
    wn.onkeypress(paddle_a_up, "w")
    wn.onkeypress(paddle_a_down, "s")
    wn.onkeypress(paddle_b_up, "Up")
    wn.onkeypress(paddle_b_down, "Down")
    
    def on_close():
        global running
        running = False
    
    root.protocol("WM_DELETE_WINDOW", on_close)
    
    running = True
    #main game loop
    while running:
        wn.update()
    
    
    
        #move the ball
        ball.setx(ball.xcor()+ball.dx)
        ball.sety(ball.ycor()+ball.dy)
    
        #border
        if ball.ycor() > 280:
            ball.sety(280)
            ball.dy*=-1
    
        if ball.ycor() < -280:
            ball.sety(-280)
            ball.dy*=-1
    
        if ball.xcor() > 380:
            ball.goto(0,0)
            ball.dx*=-1
            score_a+=1
            pen.clear()
            pen.write(f"Player A: {score_a} Player B: {score_b}", align="center", font=('Courier', 24, "normal"))
    
        if ball.xcor() < -380:
            ball.goto(0,0)
            ball.dx*=-1
            score_b += 1
            pen.clear()
            pen.write(f"Player A: {score_a} Player B: {score_b}", align="center", font=('Courier', 24, "normal"))
    
        #colliosion
        if (ball.xcor() > 330 and ball.xcor() < 340) and (ball.ycor() < paddle_b.ycor() + 50 and ball.ycor() > paddle_b.ycor() -50):
            ball.setx(330)
            ball.dx*=-1
        if (ball.xcor() < -330 and ball.xcor() > -340) and (ball.ycor() < paddle_a.ycor() + 50 and ball.ycor() > paddle_a.ycor() -50):
            ball.setx(-330)
            ball.dx*=-1
    

    【讨论】:

      【解决方案2】:

      我们可以从turtle 内部解决这个问题,无需下降到tkinter 基础。一般来说,你需要遵守规则。具体来说,没有while running: aka while True:。 Turtle 是一个基于事件的环境,您不会让事件得到正确处理,从而导致错误消息。

      我们需要一个ontimer() 事件,而不是while 循环:

      from turtle import Screen, Turtle
      
      FONT = ('Courier', 24, 'normal')
      
      # Score
      score_a = 0
      score_b = 0
      
      # Function
      def paddle_a_up():
          paddle_a.sety(paddle_a.ycor() + 20)
      
      def paddle_a_down():
          paddle_a.sety(paddle_a.ycor() - 20)
      
      def paddle_b_up():
          paddle_b.sety(paddle_b.ycor() + 20)
      
      def paddle_b_down():
          paddle_b.sety(paddle_b.ycor() - 20)
      
      def single_step():
          global score_a, score_b
      
          # move the ball
          ball.setposition(ball.xcor() + ball.dx, ball.ycor() + ball.dy)
      
          # border
          if ball.ycor() > 280:
              ball.sety(280)
              ball.dy *= -1
          elif ball.ycor() < -280:
              ball.sety(-280)
              ball.dy *= -1
      
          if ball.xcor() > 380:
              ball.goto(0, 0)
              ball.dx *= -1
              score_a += 1
              pen.clear()
              pen.write(f"Player A: {score_a} Player B: {score_b}", align='center', font=FONT)
      
          if ball.xcor() < -380:
              ball.goto(0, 0)
              ball.dx *= -1
              score_b += 1
              pen.clear()
              pen.write(f"Player A: {score_a} Player B: {score_b}", align='center', font=FONT)
      
          # collision
          if 330 < ball.xcor() < 340 and paddle_b.ycor() - 50 < ball.ycor() < paddle_b.ycor() + 50:
              ball.setx(330)
              ball.dx *= -1
          elif -340 < ball.xcor() < -330 and paddle_a.ycor() - 50 < ball.ycor() < paddle_a.ycor() + 50:
              ball.setx(-330)
              ball.dx *= -1
      
          screen.update()
      
          screen.ontimer(single_step)
      
      screen = Screen()
      screen.title('Pong')
      screen.bgcolor('black')
      screen.setup(width=800, height=600)
      screen.tracer(0)
      
      # paddle A
      paddle_a = Turtle()
      paddle_a.shape('square')
      paddle_a.color('white')
      paddle_a.shapesize(stretch_wid=5, stretch_len=1)
      paddle_a.penup()
      paddle_a.setx(-350)
      
      # paddle B
      paddle_b = Turtle()
      paddle_b.shape('square')
      paddle_b.color('white')
      paddle_b.shapesize(stretch_wid=5, stretch_len=1)
      paddle_b.penup()
      paddle_b.setx(350)
      
      # Ball
      ball = Turtle()
      ball.shape('circle')
      ball.color('white')
      ball.penup()
      
      ball.dx = 1
      ball.dy = 1
      
      # pen
      pen = Turtle()
      pen.hideturtle()
      pen.color('white')
      pen.penup()
      
      pen.sety(260)
      pen.write("Player A: 0 Player B: 0", align='center', font=FONT)
      
      # Keyboard binding
      screen.onkeypress(paddle_a_up, 'w')
      screen.onkeypress(paddle_a_down, 's')
      screen.onkeypress(paddle_b_up, 'Up')
      screen.onkeypress(paddle_b_down, 'Down')
      screen.listen()
      
      single_step()
      
      screen.mainloop()
      

      现在,当您关闭窗口时,该事件将由运行游戏的同一 mainloop 事件处理程序处理。

      【讨论】:

        猜你喜欢
        • 2022-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-25
        • 1970-01-01
        • 2015-03-27
        • 2015-04-16
        • 1970-01-01
        相关资源
        最近更新 更多