-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtkinter_square.py
More file actions
89 lines (66 loc) · 2.18 KB
/
tkinter_square.py
File metadata and controls
89 lines (66 loc) · 2.18 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
80
81
82
83
84
85
86
87
88
89
import abc
import tkinter as tk
# Абстрактные класс Shape
class Shape(abc.ABC):
def __init__(self, x, y):
self.x = x
self.y = y
@abc.abstractmethod
def area(self):
pass
@abc.abstractmethod
def draw(self, canvas):
pass
def print_point(self):
print(f"X: {self.x}, Y: {self.y}")
# класс прямоугольника
class Rectangle(Shape):
def __init__(self, x, y, width, height, color="blue"):
super().__init__(x, y)
self.width = width
self.height = height
self.color = color
def area(self):
return self.width * self.height
def draw(self, canvas):
canvas.create_rectangle(
self.x,
self.y,
self.x + self.width,
self.y + self.height,
fill=self.color,
outline="black"
)
# класс квадрата
class Square(Rectangle):
def __init__(self, x, y, size, color="green"):
super().__init__(x, y, size, size, color)
class ShapeApp:
def __init__(self, root):
self.root = root
self.root.title("Графический экран")
self.canvas = tk.Canvas(root, width=500, height=500, bg="white")
self.canvas.pack()
# общее количество всех фигур на экране
self.shapes = []
# обработчик клика мышью для добавления новых объектов на экран
self.canvas.bind("<Button-1>", self.add_square)
def add_square(self, event):
size = 50
x = event.x - size // 2
y = event.y - size // 2
square = Square(x, y, size, color="skyblue")
self.shapes.append(square)
# Перерисовать все фигуры
self.redraw()
def redraw(self):
self.canvas.delete("all")
for shape in self.shapes:
shape.draw(self.canvas)
# Основная программа
def main():
root = tk.Tk()
app = ShapeApp(root)
root.mainloop()
if __name__ == "__main__":
main()