| 19 июня, суббота, 2021 | 00:05:27

Скопировано в буфер обмена!  Копировать Боковая панель Переносы строк Сырой код
1#!/usr/bin/python3
2
3import sys
4from random import randrange
5from PyQt5.QtWidgets import \
6 QLabel, QSpinBox, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QCheckBox, QFileDialog, QApplication, QSizePolicy
7from PyQt5.QtGui import QPainter, QPen, QPixmap, QImage, qRgb
8from PyQt5.QtCore import Qt
9
10class MainWindow(QWidget):
11 def __init__(self):
12 super().__init__()
13 self.resize(480, 340)
14 self.setWindowTitle('Screen Widget')
15 self.setLayout(self.createLayout())
16 self.button.clicked.connect(self.save)
17 self.regen.clicked.connect(self.canvas.regenerate)
18 self.show()
19 self.regen.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
20 self.w.setValue(self.canvas.size().width())
21 self.h.setValue(self.canvas.size().height())
22
23 def createLayout(self):
24 hbox = QHBoxLayout()
25 vbox = QVBoxLayout()
26 self.canvas = Canvas()
27 self.button = QPushButton('Save...')
28 self.regen = QPushButton('Regenerate Pixmap')
29 self.smooth = QCheckBox('Smooth')
30 self.smooth.setChecked(True)
31 self.w = QSpinBox()
32 self.h = QSpinBox()
33 self.w.setMaximum(1000)
34 self.h.setMaximum(1000)
35 hbox.addWidget(self.button)
36 hbox.addWidget(QLabel('Resolution: '))
37 hbox.addWidget(self.w)
38 hbox.addWidget(QLabel('x'))
39 hbox.addWidget(self.h)
40 hbox.addWidget(self.smooth)
41 hbox.addStretch()
42 vbox.addWidget(self.canvas)
43 vbox.addWidget(self.regen)
44 vbox.addLayout(hbox)
45 return vbox
46
47 def save(self):
48 path, ok = QFileDialog.getSaveFileName(self, caption='Save Image As...', filter='Images (*.png)')
49 if ok:
50 if not path.endswith('.png') and not path.endswith('.PNG'):
51 path += '.png'
52 pixmap = self.canvas.grab()
53 pixmap_to_save = pixmap.scaled(self.w.value(), self.h.value(), \
54 transformMode = Qt.SmoothTransformation if self.smooth.isChecked() else Qt.FastTransformation)
55 pixmap_to_save.save(path, 'PNG')
56
57class Canvas(QWidget):
58 def __init__(self):
59 super().__init__()
60 self.pixmap = self.generatePixmap()
61
62 def paintEvent(self, event):
63 qp = QPainter(self)
64
65 # Text
66 qp.setPen(Qt.red)
67 for i in range(0, 10):
68 qp.drawText(20, i * 20, 'Test: ' + str(i))
69
70 # Lines
71 qp.setPen(QPen(Qt.blue, 5))
72 for i in range(0, 10):
73 qp.drawLine(100, i * 20 + 10, 400, i * 20 + 10)
74
75 # Pixmap
76 qp.drawPixmap(420, 20, self.pixmap)
77
78 def generatePixmap(self):
79 image = QImage(100, 100, QImage.Format_RGB32)
80 for i in range(0, 100):
81 for j in range(0, 100):
82 image.setPixel(i, j, qRgb(randrange(256), randrange(256), randrange(256)))
83 return QPixmap.fromImage(image)
84
85 def regenerate(self):
86 self.pixmap = self.generatePixmap()
87 self.update()
88
89def main():
90 app = QApplication(sys.argv)
91 win = MainWindow()
92 sys.exit(app.exec_())
93
94if __name__ == '__main__':
95 main()
```
Язык
Библиотека
VM
Скорость
JavaScript 20.3.0
Highlight.js 10.4.1
GraalVM
Быстро
Ruby 2.6.6
Rouge 3.26.0
GraalVM
Приемлемо
Время рендеринга подсветки синтаксиса на сервере: 6008ms.
```
Версия Code Polyglot: 1.0.0, 40b65db_190, 19-Jan-2021 15:55:44