Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from random import randrange as rnd, choice
- from PIL import ImageTk, Image
- import os, sys
- import time
- root = Tk()
- width = 900
- hight = 600
- root.geometry(str(width)+'x'+str(hight)+'+100+100')
- canv = Canvas(bg='lightblue')
- canv.pack(fill=BOTH,expand=1)
- def pointInSegment(x,x1,x2):
- return x1 <= x <= x2
- def segmentInSegment(x1,x2,xx1,xx2):
- return pointInSegment(x1,xx1,xx2) or pointInSegment(x2,xx1,xx2) or \
- pointInSegment(xx1,x1,x2) or pointInSegment(xx2,x1,x2)
- def rectInRect(x1,y1,x2,y2,xx1,yy1,xx2,yy2):
- if segmentInSegment(x1,x2,xx1,xx2) and segmentInSegment(y1,y2,yy1,yy2):
- return True
- if segmentInSegment(xx1,xx2,x1,x2) and segmentInSegment(yy1,yy2,y1,y2):
- return True
- return False
- xx1,yy1,xx2,yy2 = 250,200,350,300
- canv.create_rectangle(xx1,yy1,xx2,yy2)
- x1,y1,x2,y2 = 0,0,0,0
- px1,py1,px2,py2 = 0,0,0,0
- sq = canv.create_rectangle(x1,y1,x2,y2)
- def m(event):
- global x1,x2,y1,y2
- px1,py1,px2,py2 = x1,y1,x2,y2
- x1 = event.x
- x2 = event.x + 150
- y1 = event.y
- y2 = event.y + 150
- color = 'black'
- if rectInRect(x1,y1,x2,y2,xx1,yy1,xx2,yy2):
- color = 'yellow'
- if segmentInSegment(px1,px2,xx1,xx2):
- # ~ print('up-down')
- if py2 < yy1:
- color = 'blue'
- print('up')
- else:
- color = 'green'
- print('down')
- else:
- # ~ print('left-right')
- if px2 < xx1:
- color = 'orange'
- print('left')
- else:
- color ='red'
- print('right')
- x1,y1,x2,y2 = px1,py1,px2,py2
- canv.coords(sq,x1,y1,x2,y2)
- canv.itemconfig(sq,fill=color)
- root.bind('<Motion>',m)
- mainloop()
Advertisement
Add Comment
Please, Sign In to add comment