-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaqua_rect.pyw
100 lines (67 loc) · 3.06 KB
/
aqua_rect.pyw
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import tkinter as tk
from tkinter import messagebox, filedialog, colorchooser
from random import randint as rnd
from PIL import ImageGrab
root = tk.Tk()
figure = 0
root.title('Webdriver Torso')
canvas = tk.Canvas(root, width=820, height=550, bg="#ffffff")
color1 = "#ff0000"
color2 = "#0000ff"
def webdrivertorso(e=None):
global figure
figure += 1
root.title('Webdriver Torso - Figure {figure}'.format(figure=figure))
canvas.delete('all')
a = canvas.create_rectangle(rnd(0, 820), rnd(0, 550), rnd(0,820), rnd(0, 550), width=0, fill=color1)
b = canvas.create_rectangle(rnd(0, 820), rnd(0, 550), rnd(0,820), rnd(0, 550), width=0, fill=color2)
def savewebdrivertorso(e=None):
x = root.winfo_rootx() + canvas.winfo_x()
y = root.winfo_rooty() + canvas.winfo_y()
x1 = x + canvas.winfo_width()
y1 = y + canvas.winfo_height()
ImageGrab.grab(bbox=(x, y, x1, y1)).save("figures/figure{figure}.png".format(figure=figure))
messagebox.showinfo(title="Image saved", message="The image is successfully saved.")
def saveaswebdrivertorso(e=None):
dialog = filedialog.asksaveasfilename(filetypes=(("PNG file", "*.png"), ("JPG file", "*.jpg")))
if dialog:
x = root.winfo_rootx() + canvas.winfo_x()
y = root.winfo_rooty() + canvas.winfo_y()
x1 = x + canvas.winfo_width()
y1 = y + canvas.winfo_height()
if not (dialog.endswith(".png") or dialog.endswith(".jpg")):
dialog += ".png"
ImageGrab.grab(bbox=(x, y, x1, y1)).save(dialog)
messagebox.showinfo(title="Image saved", message="The image is successfully saved.")
genbtn = tk.Button(root, command=webdrivertorso, text="Generate")
menubar = tk.Menu(root)
root.config(menu=menubar)
file_menu = tk.Menu(menubar)
file_menu.add_command(label="Save", command=savewebdrivertorso)
file_menu.add_command(label="Save As...", command=saveaswebdrivertorso)
menubar.add_cascade(label="File", menu=file_menu)
def rectcolors():
global color1, color2
colordialog1 = colorchooser.askcolor()
colordialog2 = colorchooser.askcolor()
if colordialog1:
color1 = colordialog1[1]
if colordialog2:
color2 = colordialog2[1]
messagebox.showinfo(title="Colors changed", message="The rectangle colors are successfully changed.")
def canvascolor():
colordialog = colorchooser.askcolor()
if colordialog:
canvas.configure(bg=colordialog[1])
messagebox.showinfo(title="Background color changed", message="The background color is successfuly changed.")
view_menu = tk.Menu(menubar)
view_menu.add_command(label="Change rectangle colors...", command=rectcolors)
view_menu.add_command(label="Change background color...", command=canvascolor)
menubar.add_cascade(label="View", menu=view_menu)
canvas.grid(row=0, column=0)
genbtn.grid(row=1, column=0)
root.bind("<Control-n>", webdrivertorso)
root.bind("<Control-s>", savewebdrivertorso)
root.bind("<Control-S>", saveaswebdrivertorso)
webdrivertorso()
root.mainloop()