Pyqt 屏幕截图工具

从Pyqt的examples中看到一段截图代码, (路径:examplas\desktop\screenshot.py)
所以想自己UI下界面,手动练习下

通过UI生成的: Screenshot.py

 # -*- coding: utf-8 -*-

 # Form implementation generated from reading ui file 'Screenshot.ui'
#
# Created: Mon Mar 30 11:41:46 2015
# by: PyQt4 UI code generator 4.10.3
#
# WARNING! All changes made in this file will be lost! from PyQt4 import QtCore, QtGui try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig) class Ui_ScrShot(object):
def setupUi(self, ScrShot):
ScrShot.setObjectName(_fromUtf8("ScrShot"))
ScrShot.resize(270, 270)
self.verticalLayout = QtGui.QVBoxLayout(ScrShot)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.labelShow = QtGui.QLabel(ScrShot)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.labelShow.sizePolicy().hasHeightForWidth())
self.labelShow.setSizePolicy(sizePolicy)
self.labelShow.setText(_fromUtf8(""))
self.labelShow.setObjectName(_fromUtf8("labelShow"))
self.verticalLayout.addWidget(self.labelShow)
self.groupBox = QtGui.QGroupBox(ScrShot)
self.groupBox.setObjectName(_fromUtf8("groupBox"))
self.verticalLayout_2 = QtGui.QVBoxLayout(self.groupBox)
self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
self.horizontalLayout_2 = QtGui.QHBoxLayout()
self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
self.labelSpinBox = QtGui.QLabel(self.groupBox)
self.labelSpinBox.setObjectName(_fromUtf8("labelSpinBox"))
self.horizontalLayout_2.addWidget(self.labelSpinBox)
self.spinBox = QtGui.QSpinBox(self.groupBox)
self.spinBox.setObjectName(_fromUtf8("spinBox"))
self.horizontalLayout_2.addWidget(self.spinBox)
self.verticalLayout_2.addLayout(self.horizontalLayout_2)
self.checkBoxHideThis = QtGui.QCheckBox(self.groupBox)
self.checkBoxHideThis.setObjectName(_fromUtf8("checkBoxHideThis"))
self.verticalLayout_2.addWidget(self.checkBoxHideThis)
self.verticalLayout.addWidget(self.groupBox)
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
spacerItem = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.pushButtonNew = QtGui.QPushButton(ScrShot)
self.pushButtonNew.setObjectName(_fromUtf8("pushButtonNew"))
self.horizontalLayout.addWidget(self.pushButtonNew)
self.pushButtonSave = QtGui.QPushButton(ScrShot)
self.pushButtonSave.setObjectName(_fromUtf8("pushButtonSave"))
self.horizontalLayout.addWidget(self.pushButtonSave)
self.pushButton_Quit = QtGui.QPushButton(ScrShot)
self.pushButton_Quit.setObjectName(_fromUtf8("pushButton_Quit"))
self.horizontalLayout.addWidget(self.pushButton_Quit)
self.verticalLayout.addLayout(self.horizontalLayout) self.retranslateUi(ScrShot)
QtCore.QMetaObject.connectSlotsByName(ScrShot) def retranslateUi(self, ScrShot):
ScrShot.setWindowTitle(_translate("ScrShot", "Form", None))
self.groupBox.setTitle(_translate("ScrShot", "Options", None))
self.labelSpinBox.setText(_translate("ScrShot", "Screenstot Delay:", None))
self.checkBoxHideThis.setText(_translate("ScrShot", "Hide This Window", None))
self.pushButtonNew.setText(_translate("ScrShot", "新建", None))
self.pushButtonSave.setText(_translate("ScrShot", "保存", None))
self.pushButton_Quit.setText(_translate("ScrShot", "退出", None)) if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
ScrShot = QtGui.QWidget()
ui = Ui_ScrShot()
ui.setupUi(ScrShot)
ScrShot.show()
sys.exit(app.exec_())

 逻辑代码; MainScreenShot.py

 # -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
from Screenshot import Ui_ScrShot
import sys
import icoqrc
class MainFrom(QtGui.QWidget):
def __init__(self):
super(MainFrom, self).__init__()
self.Ui = Ui_ScrShot()
self.Ui.setupUi(self)
self.Ui.labelShow.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding) # size 策略为 可扩展 expanding
self.Ui.labelShow.setAlignment(QtCore.Qt.AlignCenter) #alignment 对齐方式 居中
self.Ui.labelShow.setMinimumSize(240, 160) # 最小为 240 X 160
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint) # 设置总是在最前
self.setWindowTitle(u'截图工具')
self.setWindowIcon(QtGui.QIcon(':qq.ico')) self.shootScreen()
self.Ui.spinBox.setSuffix(' s')
self.Ui.spinBox.setMaximum(60)
self.Ui.spinBox.setValue(5) self.Ui.pushButtonNew.clicked.connect(self.newScreenshot) # 从新开始新建截图
self.Ui.pushButtonSave.clicked.connect(self.saveScreenshot) # 保存截图
self.Ui.pushButton_Quit.clicked.connect(self.close) # 退出 # 截图
def shootScreen(self):
if self.Ui.spinBox.value() != 0:
QtGui.qApp.beep() #操作带上系统的响铃
self.originalPixmap = QtGui.QPixmap.grabWindow(QtGui.QApplication.desktop().winId()) # 获取 屏幕桌面截图
self.updateScreenshotLabel()
self.Ui.pushButtonNew.setDisabled(False)
if self.Ui.checkBoxHideThis.isChecked(): # 当选择隐藏按钮为True时, 截图完成显示窗体
self.show() # 获取图片显示在label上
def updateScreenshotLabel(self):
# self.originalPixmap.scaled() scaled()函数的声明const返回一个Qpixmap
# QtCore.Qt.KeepAspectRatio 尽可能大的在一个给定的矩形大小缩放到一个矩形且保持长宽比。
# QtCore.Qt.SmoothTransformation 平滑转换
self.Ui.labelShow.setPixmap(self.originalPixmap.scaled(self.Ui.labelShow.size(), QtCore.Qt.KeepAspectRatio, QtCore.Qt.SmoothTransformation)) # 保存截图图片
def saveScreenshot(self):
format = 'png'
initialPath = QtCore.QDir.currentPath() + "/untitled." + format fileName = QtGui.QFileDialog.getSaveFileName(self, u"另存为",
initialPath,
"%s Files (*.%s)" % (format.upper(), format))
if fileName:
self.originalPixmap.save(fileName, format) # 新创建截图
def newScreenshot(self):
if self.Ui.checkBoxHideThis.isChecked():
self.hide()
self.Ui.pushButtonNew.setDisabled(True)
QtCore.QTimer.singleShot(self.Ui.spinBox.value() * 1000, self.shootScreen) # * 秒后触发截图 # 重载 resizeEvent 方法
def resizeEvent(self, event):
scaledSize = self.originalPixmap.size()
scaledSize.scale(self.Ui.labelShow.size(), QtCore.Qt.KeepAspectRatio)
if not self.Ui.labelShow.pixmap() or scaledSize != self.Ui.labelShow.pixmap().size(): # 当pixmap改变大小时候重新加载updateScreenshotLabel
self.updateScreenshotLabel() if __name__ == '__main__':
App = QtGui.QApplication(sys.argv)
MainApp = MainFrom()
MainApp.show()
sys.exit(App.exec_())

效果:

Pyqt 屏幕截图工具

上一篇:过滤器复用代码【中文乱码、HTML转义】


下一篇:python3 json模块