Здравствуйте!
Нашел исходный файлы (.py) программы для создания скриншотов. Там, как я понял используется расширение PyQt.
Сам только начал изучать всю эту кухню, но время жмет. Буду очень благодарен, если поможете разобраться в коде. Код прилагаю.
from PyQt5.QtCore import QDir, Qt, QTimer
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import (QApplication, QCheckBox, QFileDialog, QGridLayout,
QGroupBox, QHBoxLayout, QLabel, QPushButton, QSizePolicy, QSpinBox,
QVBoxLayout, QWidget)
class Screenshot(QWidget):
def __init__(self):
super(Screenshot, self).__init__()
self.screenshotLabel = QLabel()
self.screenshotLabel.setSizePolicy(QSizePolicy.Expanding,
QSizePolicy.Expanding)
self.screenshotLabel.setAlignment(Qt.AlignCenter)
self.screenshotLabel.setMinimumSize(240, 160)
self.createOptionsGroupBox()
self.createButtonsLayout()
mainLayout = QVBoxLayout()
mainLayout.addWidget(self.screenshotLabel)
mainLayout.addWidget(self.optionsGroupBox)
mainLayout.addLayout(self.buttonsLayout)
self.setLayout(mainLayout)
self.shootScreen()
self.delaySpinBox.setValue(5)
self.setWindowTitle("TakeScreenshot")
self.resize(300, 200)
def resizeEvent(self, event):
scaledSize = self.originalPixmap.size()
scaledSize.scale(self.screenshotLabel.size(), Qt.KeepAspectRatio)
if not self.screenshotLabel.pixmap() or scaledSize != self.screenshotLabel.pixmap().size():
self.updateScreenshotLabel()
def newScreenshot(self):
if self.hideThisWindowCheckBox.isChecked():
self.hide()
self.newScreenshotButton.setDisabled(True)
QTimer.singleShot(self.delaySpinBox.value() * 1000,
self.shootScreen)
def saveScreenshot(self):
format = 'png'
initialPath = QDir.currentPath() + "/untitled." + format
fileName, _ = QFileDialog.getSaveFileName(self, "Сохранить как", initialPath,
"%s Files (*.%s);;All Files (*)" % (format.upper(), format))
if fileName:
self.originalPixmap.save(fileName, format)
def shootScreen(self):
if self.delaySpinBox.value() != 0:
QApplication.instance().beep()
screen = QApplication.primaryScreen()
if screen is not None:
self.originalPixmap = screen.grabWindow(0)
else:
self.originalPixmap = QPixmap()
self.updateScreenshotLabel()
self.newScreenshotButton.setDisabled(False)
if self.hideThisWindowCheckBox.isChecked():
self.show()
def updateCheckBox(self):
if self.delaySpinBox.value() == 0:
self.hideThisWindowCheckBox.setDisabled(True)
else:
self.hideThisWindowCheckBox.setDisabled(False)
def createOptionsGroupBox(self):
self.optionsGroupBox = QGroupBox("Опции")
self.delaySpinBox = QSpinBox()
self.delaySpinBox.setSuffix(" сек")
self.delaySpinBox.setMaximum(60)
self.delaySpinBox.valueChanged.connect(self.updateCheckBox)
self.delaySpinBoxLabel = QLabel("Задержка:")
self.hideThisWindowCheckBox = QCheckBox("Скрыть окно")
optionsGroupBoxLayout = QGridLayout()
optionsGroupBoxLayout.addWidget(self.delaySpinBoxLabel, 0, 0)
optionsGroupBoxLayout.addWidget(self.delaySpinBox, 0, 1)
optionsGroupBoxLayout.addWidget(self.hideThisWindowCheckBox, 1, 0, 1, 2)
self.optionsGroupBox.setLayout(optionsGroupBoxLayout)
def createButtonsLayout(self):
self.newScreenshotButton = self.createButton("Новый скриншот",
self.newScreenshot)
self.saveScreenshotButton = self.createButton("Сохранить",
self.saveScreenshot)
self.quitScreenshotButton = self.createButton("Выход", self.close)
self.buttonsLayout = QHBoxLayout()
self.buttonsLayout.addStretch()
self.buttonsLayout.addWidget(self.newScreenshotButton)
self.buttonsLayout.addWidget(self.saveScreenshotButton)
self.buttonsLayout.addWidget(self.quitScreenshotButton)
def createButton(self, text, member):
button = QPushButton(text)
button.clicked.connect(member)
return button
def updateScreenshotLabel(self):
self.screenshotLabel.setPixmap(self.originalPixmap.scaled(
self.screenshotLabel.size(), Qt.KeepAspectRatio,
Qt.SmoothTransformation))
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
screenshot = Screenshot()
screenshot.show()
sys.exit(app.exec_())