-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuttons.py
More file actions
53 lines (35 loc) · 1.09 KB
/
buttons.py
File metadata and controls
53 lines (35 loc) · 1.09 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
import tkinter
from random import randint
WIDTH = 500
HEIGHT = 500
MASTER = tkinter.Tk()
MASTER.title("Buttons")
CANVAS = tkinter.Canvas(MASTER, bg="white", width=WIDTH, height=HEIGHT)
current = None
def random_color() -> str:
s = '#'
for part in (randint(0, 255), randint(0, 255), randint(0, 255)):
part = hex(part)[2:]
if len(part) == 1:
s += '0'
s += part
return s
def create_new(event: tkinter.Event = None) -> None:
global current
CANVAS.delete(current)
x = randint(2, WIDTH - 8)
y = randint(2, HEIGHT - 8)
width = randint(10, WIDTH + 1 - x)
height = randint(10, HEIGHT + 1 - y)
color = random_color()
if randint(0, 1):
current = CANVAS.create_rectangle(x, y, x + width, y + height, fill=color)
else:
current = CANVAS.create_oval(x, y, x + width, y + height, fill=color)
CANVAS.tag_bind(current, "<Button-1>", create_new)
def main() -> None:
create_new()
CANVAS.pack()
MASTER.mainloop()
if __name__ == '__main__':
main()