-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathFractal_tree__Qt_gui.py
102 lines (76 loc) · 2.15 KB
/
Fractal_tree__Qt_gui.py
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
101
102
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "ipetrash"
"""
Фрактальное дерево / Fractal tree
"""
import math
import io
from PIL import Image, ImageDraw
try:
from PyQt5.QtWidgets import (
QApplication,
QWidget,
QLabel,
QPushButton,
QVBoxLayout,
QSizePolicy,
)
from PyQt5.QtGui import QImage, QPixmap, QPainter
from PyQt5.QtCore import Qt
except ImportError:
try:
from PyQt4.QtGui import (
QImage,
QPainter,
QApplication,
QWidget,
QLabel,
QPushButton,
QVBoxLayout,
QSizePolicy,
QPixmap,
)
from PyQt4.QtCore import Qt
except ImportError:
from PySide.QtGui import (
QImage,
QPainter,
QApplication,
QWidget,
QLabel,
QPushButton,
QVBoxLayout,
QSizePolicy,
QPixmap,
)
from PySide.QtCore import Qt
from Fractal_tree__PIL import draw_fractal_tree
class Widget(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Fractal tree")
self.img_label = QLabel()
self.img_label.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.generate_tree_button = QPushButton("Generate Tree")
self.generate_tree_button.clicked.connect(self.generate_tree)
main_layout = QVBoxLayout()
main_layout.addWidget(self.generate_tree_button)
main_layout.addWidget(self.img_label)
self.setLayout(main_layout)
def generate_tree(self):
img = Image.new("RGB", (700, 600), "white")
draw_fractal_tree(ImageDraw.Draw(img), 350, 580, 3 * math.pi / 2, 200)
img_bytes_io = io.BytesIO()
img.save(img_bytes_io, format="PNG")
img_bytes = img_bytes_io.getvalue()
img = QPixmap()
img.loadFromData(img_bytes)
self.img_label.setPixmap(img)
if __name__ == "__main__":
app = QApplication([])
w = Widget()
w.resize(700, 600)
w.show()
w.generate_tree()
app.exec()