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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| try:
import Tkinter as tk
except:
import tkinter as tk
import random
root = tk.Tk()
root.title("Demo - Canvas")
width = 100
height = 50
can = tk.Canvas(root,bg="white", width=width, height = height)
can.pack()
lst_items = []
started = False
start_pos = None
for i in range(50):
x = random.randint(0, width)
y = random.randint(0, height)
can.create_text(x, 0, text="A")
def start(event=None):
global started, start_pos
start_pos = event.x, event.y
started = True
print("started")
def end(event=None):
global lst_items, started
print("ended")
started = False
for item in lst_items:
can.itemconfig(item, fill="black")
lst_items = []
def update(event=None):
global started, lst_items
print (started)
if started:
"""
x, y = event.x, event.y
"""
a,b = start_pos
c,d = event.x, event.y
tmp = can.find_overlapping(a,b,c,d)
for item in lst_items:
can.itemconfig(item, fill="black")
for item in tmp:
can.itemconfig(item, fill="green")
lst_items = tmp
can.bind("<Motion>", update)
can.bind("<ButtonPress>", start)
can.bind("<ButtonRelease>", end)
root.mainloop() |
Partager