I am trying to use classes to create ovals(circles) in canvas. I am a beginner and came with the following code. But the ball does not show and it does not move. I placed prints in between codes to see which codes get executed. Can someone tell me how I could fix this. Thanks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
from tkinter import * import random import time tk = Tk() tk.title( "classy collision detection" ) width = 700 height = 500 x1, y1 = 10 , 10 x2, y2 = 60 , 60 canvas = Canvas(tk, width = width, height = height, bg = 'pink' ) canvas.pack() xspeed = 10 yspeed = 10 class Ball: def __init__( self , ball, colors): self .ball = ball self .colors = random.choice([ "red" , "blue" , "green" , \ "yellow" , "brown" , "cyan" ]) self .colors.pack() ball = canvas.create_oval(x1, y1, x2, y2, fill = colors) ball.pack() print ( "y" ) def move_ball( self ): print ( "s" ) canvas.move(ball, xspeed, yspeed) print ( "h" ) move = Ball.move_ball move print ( "k" ) tk.update() time.sleep( 0.05 ) canvas.mainloop() |