且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当我尝试退出应用程序窗口时,Python 返回错误

更新时间:2023-10-23 07:52:57

看来turtle 使用tkinter.在 Tk 中有一个名为协议的方法,您可以使用它来控制发生某些事件时会发生什么,其中一个事件是点击红色的X".

It looks like turtle uses tkinter. In Tk there's a method named protocol which you can use to control what happens when certain events happen, one of those being hitting the red 'X'.

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

In order to invoke that we need to get the root level window.

放置

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

将根设置为您的根窗口.

will set root as your root window.

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

From here you can use root.protocol("WM_DELETE_WINDOW", on_close) to invoke a function named on_close or whatever you want to name it.

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

Your function will probably look something like this.

def on_close():
    global running
    running = False

这将使您摆脱循环并关闭您的程序.

This will break you out of your loop and close your program.

完整代码.

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