我如何获得例外处理来为Tclerror工作?

发布于 2025-02-08 14:29:18 字数 1530 浏览 1 评论 0原文

因此,除最后2行代码和idk为什么... 有人知道如何做这项工作吗?

from tkinter import *
import time

class Ball:
    def __init__(self, canvas,x,y,diameter,diameter1,xVelocity,yVelocity,color):
        self.canvas = canvas
        self.image = canvas.create_oval(x,y,diameter,diameter1,fill=color)
        self.xVelocity = xVelocity
        self.yVelocity = yVelocity

    def move(self):
        coordinates = self.canvas.coords(self.image)
        if(coordinates[2]>=(self.canvas.winfo_width()) or coordinates[0]<0):
            self.xVelocity = -self.xVelocity
        if(coordinates[3]>=(self.canvas.winfo_height()) or coordinates[1]<0):
            self.yVelocity = -self.yVelocity

        self.canvas.move(self.image,self.xVelocity,self.yVelocity)


try:
    root = Tk()
    WIDTH = 500
    HEIGHT = 500

    canvas = Canvas(root, width=WIDTH, height=HEIGHT, bg="grey")
    canvas.pack()

    volley_ball = Ball(canvas, 0, 0, 100, 50, 1, 1, "white")
    tennis_ball = Ball(canvas, 0, 0, 50, 50, 4, 3, "yellow")
    basket_ball = Ball(canvas, 0, 0, 125, 125, 3, 5, "orange")
    bowling_ball = Ball(canvas, 0, 0, 75, 75, 2, 1, "black")

    def main():
        while True:
            volley_ball.move()
            tennis_ball.move()
            basket_ball.move()
            bowling_ball.move()
            root.update()
            time.sleep(0.01)

    root.after(0, main)
    root.after(3000, root.destroy)
    mainloop()

# I don't know how get this part to work:
except TclError:
    print("something")

So everything is working except the last 2 line of code and idk why...
Is anybody know how can I make this work?

from tkinter import *
import time

class Ball:
    def __init__(self, canvas,x,y,diameter,diameter1,xVelocity,yVelocity,color):
        self.canvas = canvas
        self.image = canvas.create_oval(x,y,diameter,diameter1,fill=color)
        self.xVelocity = xVelocity
        self.yVelocity = yVelocity

    def move(self):
        coordinates = self.canvas.coords(self.image)
        if(coordinates[2]>=(self.canvas.winfo_width()) or coordinates[0]<0):
            self.xVelocity = -self.xVelocity
        if(coordinates[3]>=(self.canvas.winfo_height()) or coordinates[1]<0):
            self.yVelocity = -self.yVelocity

        self.canvas.move(self.image,self.xVelocity,self.yVelocity)


try:
    root = Tk()
    WIDTH = 500
    HEIGHT = 500

    canvas = Canvas(root, width=WIDTH, height=HEIGHT, bg="grey")
    canvas.pack()

    volley_ball = Ball(canvas, 0, 0, 100, 50, 1, 1, "white")
    tennis_ball = Ball(canvas, 0, 0, 50, 50, 4, 3, "yellow")
    basket_ball = Ball(canvas, 0, 0, 125, 125, 3, 5, "orange")
    bowling_ball = Ball(canvas, 0, 0, 75, 75, 2, 1, "black")

    def main():
        while True:
            volley_ball.move()
            tennis_ball.move()
            basket_ball.move()
            bowling_ball.move()
            root.update()
            time.sleep(0.01)

    root.after(0, main)
    root.after(3000, root.destroy)
    mainloop()

# I don't know how get this part to work:
except TclError:
    print("something")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情释 2025-02-15 14:29:18

现在尝试一下,它不会阻止root.destroy感谢ACW1668指出了这一点!

from tkinter import *
import time

class Ball:
    def __init__(self, canvas,x,y,diameter,diameter1,xVelocity,yVelocity,color):
        self.canvas = canvas
        self.image = canvas.create_oval(x,y,diameter,diameter1,fill=color)
        self.xVelocity = xVelocity
        self.yVelocity = yVelocity

    def move(self):
        coordinates = self.canvas.coords(self.image)
        if(coordinates[2]>=(self.canvas.winfo_width()) or coordinates[0]<0):
            self.xVelocity = -self.xVelocity
        if(coordinates[3]>=(self.canvas.winfo_height()) or coordinates[1]<0):
            self.yVelocity = -self.yVelocity

        self.canvas.move(self.image,self.xVelocity,self.yVelocity)


try:
    root = Tk()
    WIDTH = 500
    HEIGHT = 500

    canvas = Canvas(root, width=WIDTH, height=HEIGHT, bg="grey")
    canvas.pack()

    volley_ball = Ball(canvas, 0, 0, 100, 50, 1, 1, "white")
    tennis_ball = Ball(canvas, 0, 0, 50, 50, 4, 3, "yellow")
    basket_ball = Ball(canvas, 0, 0, 125, 125, 3, 5, "orange")
    bowling_ball = Ball(canvas, 0, 0, 75, 75, 2, 1, "black")

    def main():
        root.after(3000, root.destroy)
        while True:
            volley_ball.move()
            tennis_ball.move()
            basket_ball.move()
            bowling_ball.move()
            root.update()
            time.sleep(0.01)

    main() # -- Added Line
# I don't know how get this part to work:
except TclError:
    print("something")


定义后调用main()函数
并将root.fer.fter(3000,root.destroy)放在main()函数中完成工作!

Try This One, Now, its not gonna block the root.destroy Thanks acw1668 for pointing that out!

from tkinter import *
import time

class Ball:
    def __init__(self, canvas,x,y,diameter,diameter1,xVelocity,yVelocity,color):
        self.canvas = canvas
        self.image = canvas.create_oval(x,y,diameter,diameter1,fill=color)
        self.xVelocity = xVelocity
        self.yVelocity = yVelocity

    def move(self):
        coordinates = self.canvas.coords(self.image)
        if(coordinates[2]>=(self.canvas.winfo_width()) or coordinates[0]<0):
            self.xVelocity = -self.xVelocity
        if(coordinates[3]>=(self.canvas.winfo_height()) or coordinates[1]<0):
            self.yVelocity = -self.yVelocity

        self.canvas.move(self.image,self.xVelocity,self.yVelocity)


try:
    root = Tk()
    WIDTH = 500
    HEIGHT = 500

    canvas = Canvas(root, width=WIDTH, height=HEIGHT, bg="grey")
    canvas.pack()

    volley_ball = Ball(canvas, 0, 0, 100, 50, 1, 1, "white")
    tennis_ball = Ball(canvas, 0, 0, 50, 50, 4, 3, "yellow")
    basket_ball = Ball(canvas, 0, 0, 125, 125, 3, 5, "orange")
    bowling_ball = Ball(canvas, 0, 0, 75, 75, 2, 1, "black")

    def main():
        root.after(3000, root.destroy)
        while True:
            volley_ball.move()
            tennis_ball.move()
            basket_ball.move()
            bowling_ball.move()
            root.update()
            time.sleep(0.01)

    main() # -- Added Line
# I don't know how get this part to work:
except TclError:
    print("something")


Calling the main() function after it is defined
And putting the root.after(3000, root.destroy) in the main() function did the Job!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文