from tkinter import *
from random import randrange as rnd
def new_rect():
global x,y,rect
canvas.delete(rect)
x = rnd(100,700)
y = rnd(100,500)
rect = canvas.create_rectangle(x,y,x+100,y+100,outline="#fb0", fill="#fb0")
root.after(1000,new_rect)
def click(event):
global points, text
canvas.delete(text)
canvas.delete(rect)
if (event.x - x >= 0
and event.x - x <= 100
and event.y - y >= 0
and event.y - y <= 100
):
points += 1
text = canvas.create_text(20,20,text=str(points), font = 'Arial 20')
root = Tk()
root.geometry('800x600')
canvas = Canvas(root,bg='white')
canvas.pack(fill=BOTH,expand=1)
x = 0
y = 0
rect = canvas.create_rectangle(x,y,x+100,y+100,outline="#fb0", fill="#fb0")
text = canvas.create_text(20,20,text=0, font = 'Arial 20')
points = 0
new_rect()
canvas.bind('<Button-1>', click)
mainloop()
|