|
学习python有三个星期了,算是做的第一个小工具
虽是小工具,大版本有3个
1.命令行版
2.pyqt GUI版
3.pyqt GUI 线程版
期间也大大小小的bug修复n多次,几次重构(笑),算得上是“软件开发的小周期”吧。今天贴代码是是第3版:
saveCssImg.py
# coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
from saveCssImg import *
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_getImg(QtGui.QDialog):
def __init__(self, parent=None):
super(UI_getImg, self).__init__(parent)
self.setupUi(self)
self.lineEdit.setText('http://qt-project.org/static/styles/style-min0512201215101354716658.css')
self.pushButton.clicked.connect(self.goWork)
def goWork(self):
self.thread = WorkerThread()
self.thread.sinOut.connect(self.outText)
self.thread.finished.connect(self.workFinished)
cssUrl = str(self.lineEdit.text())
savePath = 'downImgs'
self.textBrowser.setText(u'准备下载图片从 %s' % cssUrl )
self.pushButton.setDisabled(True)
self.thread.setEvr(cssUrl,savePath)
def workFinished(self):
self.outText(u'已退出下载!')
self.pushButton.setDisabled(False)
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(560, 407)
self.pushButton = QtGui.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(460, 20, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.textBrowser = QtGui.QTextBrowser(Dialog)
self.textBrowser.setGeometry(QtCore.QRect(10, 60, 541, 341))
self.textBrowser.setObjectName(_fromUtf8("textBrowser"))
self.lineEdit = QtGui.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(10, 20, 431, 20))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "download css img", None))
self.pushButton.setText(_translate("Dialog", "download", None))
def outText(self,text):
self.textBrowser.append(text)
class WorkerThread(QtCore.QThread):
sinOut = QtCore.pyqtSignal(str)
def __init__(self,parent=None):
super(WorkerThread,self).__init__(parent)
def setEvr(self,cssUrl,savePath):
self.cssUrl = cssUrl
self.savePath = savePath
self.start()
def getMsg(self,msg):
self.sinOut.emit(msg)
def run(self):
self.saveimg = saveCssBackImg(self.cssUrl,self.savePath)
self.saveimg.updateSig.connect(self.getMsg)
self.saveimg.saveImg()
if __name__ == "__main__":
app = QtGui.QApplication([])
ui = UI_getImg()
ui.setWindowFlags(QtCore.Qt.MSWindowsFixedSizeDialogHint)
ui.show()
app.exec_()
project_ui.py
# coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
from saveCssImg import *
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_getImg(QtGui.QDialog):
def __init__(self, parent=None):
super(UI_getImg, self).__init__(parent)
self.setupUi(self)
self.lineEdit.setText('http://qt-project.org/static/styles/style-min0512201215101354716658.css')
self.pushButton.clicked.connect(self.goWork)
def goWork(self):
self.thread = WorkerThread()
self.thread.sinOut.connect(self.outText)
self.thread.finished.connect(self.workFinished)
cssUrl = str(self.lineEdit.text())
savePath = 'downImgs'
self.textBrowser.setText(u'准备下载图片从 %s' % cssUrl )
self.pushButton.setDisabled(True)
self.thread.setEvr(cssUrl,savePath)
def workFinished(self):
self.outText(u'已退出下载!')
self.pushButton.setDisabled(False)
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(560, 407)
self.pushButton = QtGui.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(460, 20, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.textBrowser = QtGui.QTextBrowser(Dialog)
self.textBrowser.setGeometry(QtCore.QRect(10, 60, 541, 341))
self.textBrowser.setObjectName(_fromUtf8("textBrowser"))
self.lineEdit = QtGui.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(10, 20, 431, 20))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "download css img", None))
self.pushButton.setText(_translate("Dialog", "download", None))
def outText(self,text):
self.textBrowser.append(text)
class WorkerThread(QtCore.QThread):
sinOut = QtCore.pyqtSignal(str)
def __init__(self,parent=None):
super(WorkerThread,self).__init__(parent)
def setEvr(self,cssUrl,savePath):
self.cssUrl = cssUrl
self.savePath = savePath
self.start()
def getMsg(self,msg):
self.sinOut.emit(msg)
def run(self):
self.saveimg = saveCssBackImg(self.cssUrl,self.savePath)
self.saveimg.updateSig.connect(self.getMsg)
self.saveimg.saveImg()
if __name__ == "__main__":
app = QtGui.QApplication([])
ui = UI_getImg()
ui.setWindowFlags(QtCore.Qt.MSWindowsFixedSizeDialogHint)
ui.show()
app.exec_()
差两处修复:
1.urllib.urlretrieve下载图片,对http status 404和403等也会保存成图片
2.QtGui.QTextBrower 横向滚动
|
|