我如何获得例外处理来为Tclerror工作?
因此,除最后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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
现在尝试一下,它不会阻止
root.destroy
感谢ACW1668指出了这一点!定义后调用
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!Calling the
main()
function after it is definedAnd putting the
root.after(3000, root.destroy)
in themain()
function did the Job!