How can I get exception handling to work for a TclError?

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

Upvotes: 1

Views: 233

Answers (1)

Belal Ahmed
Belal Ahmed

Reputation: 179

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!

Upvotes: 2

Related Questions