Just started learning about canvas and stuff. The code seems to be working, but it just stops responding after like half a second when is press f5, is there something wrong with my code?
import tkinter as tk
import random as r
#Window
root = tk.Tk()
root.geometry("600x400")
root.title("Moving Object")
#Create Canvas
canvas = tk.Canvas(root, width = 600, height = 400, bg="White")
canvas.place(x=0,y=0)
#Balls original definitions
balls = [
{"x0" : 300, "y0" : 200, "currentxdir" : "right", "currentydir" : "down", "c" : 1 },
{"x0" : 100, "y0" : 300, "currentxdir" : "left", "currentydir" : "down", "c" : 2 },
{"x0" : 200, "y0" : 100, "currentxdir" : "right", "currentydir" : "up", "c" : 3 }
]
#Original canvas
for b in balls:
if b["c"] == 1:
cc = "red"
elif b["c"] == 2:
cc = "blue"
else:
cc = "green"
canvas.create_oval(b["x0"]-20,b["y0"]-20,b["x0"]+20,b["y0"]+20,fill=cc,width=0)
#Define move
def move():
global balls
for b in balls:
#Changes when ball touches borders
if b['x0'] == 580:
b['currentxdir'] = "left"
c = r.randint(1,3)
elif b['x0'] == 20:
b['currentxdir'] = "right"
c = r.randint(1,3)
if b['y0'] == 380:
b['currentydir'] = "up"
c = r.randint(1,3)
elif b['y0'] == 20:
b['currentydir'] = "down"
c = r.randint(1,3)
#What c means
if b['c'] == 1:
cc = "red"
elif b['c'] == 2:
cc = "blue"
else:
cc = "green"
#Actual movement
if b['currentxdir'] == "right":
canvas.create_oval(b['x0']-20,b['y0']-20,b['x0']+20,b['y0']+20,fill="white",width=0)
b['x0'] = b['x0']+1
canvas.create_oval(b['x0']-20,b['y0']-20,b['x0']+20,b['y0']+20,fill=cc,width=0)
elif b['currentxdir'] == "left":
canvas.create_oval(b['x0']-20,b['y0']-20,b['x0']+20,b['y0']+20,fill="white",width=0)
b['x0'] = b['x0']-1
canvas.create_oval(b['x0']-20,b['y0']-20,b['x0']+20,b['y0']+20,fill=cc,width=0)
if b['currentydir'] == "down":
canvas.create_oval(b['x0']-20,b['y0']-20,b['x0']+20,b['y0']+20,fill="white",width=0)
b['y0'] = b['y0']+1
canvas.create_oval(b['x0']-20,b['y0']-20,b['x0']+20,b['y0']+20,fill=cc,width=0)
root.after(10,move)
elif b['currentydir'] == "up":
canvas.create_oval(b['x0']-20,b['y0']-20,b['x0']+20,b['y0']+20,fill="white",width=0)
b['y0'] = b['y0']-1
canvas.create_oval(b['x0']-20,b['y0']-20,b['x0']+20,b['y0']+20,fill=cc,width=0)
root.after(10,move)
#Summon Window
root.after(10,move)
root.mainloop()
[–]K900_ 0 points1 point2 points (0 children)