-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphics_events_2.py
More file actions
79 lines (60 loc) · 2.4 KB
/
graphics_events_2.py
File metadata and controls
79 lines (60 loc) · 2.4 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
##############################################################################
# The program do not nothing special, only shows events with bind
##############################################################################
import tkinter
import random
width,height = 640,480
canvas = tkinter.Canvas(width=width, height=height)
canvas.pack()
# create text
#################################
def B1_press(event):
text = "(" + str(event.x) + "," + str(event.y) + ")"
canvas.create_text(event.x, event.y, text=text, font='Helvetica 12')
# canvas.bind('<ButtonPress-1>',B1_press)
#################################
# create box with filling on click
#################################
def rect(x,y,size,count):
coordinates = x, y
for i in range(0,count):
canvas.create_rectangle(x, y, x + size, y + size)
x = x + size
def click(event):
order_x = ((event.x - x) // size)+1
order_y = ((event.y - y) // size)+1
if 0 < order_x <= count and order_y == 1:
# print("x = {} ==> x={} y={} ".format(event.x, order_x,order_y))
x0 = x + ((order_x - 1 )*size)
y0 = y + ((order_y - 1 )*size)
canvas.create_rectangle(x0, y0, x0 + size, y0 + size, fill = "red")
x,y,size,count = 100,100,50,10
# rect(x,y,size,count)
# canvas.bind('<ButtonPress-1>', click)
#################################
# create and drag square_id
#################################
def press_move(event):
global coordinates
coordinates = (event.x,event.y)
# def B1_drag(event):
# global coordinates
# if (coordinates[0]-25) < event.x < (coordinates[0]+25) and (coordinates[1]-25) < event.y < (coordinates[1]+25):
# canvas.move(square_id, event.x - coordinates[0], event.y - coordinates[1])
# coordinates = (abs(event.x), abs(event.y))
x_square, y_square = width/2, height/2
coordinates = [x_square , y_square]
# square_id = canvas.create_rectangle(x_square - 25, y_square - 25,x_square + 25,
# y_square + 25)
## canvas.bind('<ButtonPress-1>', press_move)
# canvas.bind('<B1-Motion>', B1_drag)
#################################
# random keyboard
#################################
def key(event):
random_x = random.randint(20, height - 20)
random_y = random.randint(20, height - 20)
canvas.create_text(random_x, random_y, text=event.char, font='Helvetica 12')
# canvas.bind_all('<Key>', key)
#################################
canvas.mainloop()