2021年3月31日星期三

Moving object on canvas python

I wanted to get a little square to move to the left 10 pixels every 3 seconds and my code is below. I'm not sure why it only moves once. Some help would be greatly apprecaited!

import tkinter as tk  import time     root = tk.Tk()     WIDTH = HEIGHT = 400     x1 = y1 = WIDTH / 2     canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT)  canvas.pack()     c1 = canvas.create_rectangle(x1, y1, x1 + 10, y1 + 10)  c2 = canvas.create_rectangle(x1, y1, x1 + 10, y1 + 10)        def draw_rect():      global c2      canvas.delete(c2)      c2 = canvas.create_rectangle(x1, y1, x1 + 10, y1 + 10, fill="green")        def del_rect():      canvas.delete(c1)      #canvas.create_rectangle(x1, y1, x1 + 10, y1 + 10, fill="white", opacity=0.5)    while 1:      root.update()      time.sleep(3)      del_rect()      x1 -= 10          draw_rect()      root.mainloop()  
https://stackoverflow.com/questions/66897522/moving-object-on-canvas-python April 01, 2021 at 10:01AM

没有评论:

发表评论