暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

使用PyQt编写的图标制作工具

原创 赵远吉 2021-05-19
611

使用PyQt编写的图标制作工具,需要安装python第三方包,下载地址:
https://www.modb.pro/download/92009

# -*- coding: utf-8 -*- import os from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtCore import Qt,QRectF from PyQt5.QtGui import QIcon, QPainter, QPainterPath, QBrush, QColor, QCursor from PyQt5.QtWidgets import QWidget,QApplication,QMessageBox import sys import PythonMagick # 设置按钮样式 button_style = ''' QPushButton {text-align : center; background-color : white; font: bold; border-color: gray; border-width: 1px; border-radius: 3px; padding: 2px; height : 14px; border-style: outset; font : 12px;} QPushButton:hover {text-align : center; background-color : cyan; font: bold; border-color: gray; border-width: 1px; border-radius: 5px; padding: 1px; height : 14px; border-style: outset; font : 12px;} QPushButton:pressed {text-align : center; background-color : cyan; font: bold; border-color: gray; border-width: 1px; border-radius: 5px; padding: 1px; height : 14px; border-style: outset; font : 12px;} ''' class RoundShadow(QWidget): """圆角边框类""" def __init__(self, parent=None): super(RoundShadow, self).__init__(parent) self.border_width = 8 # 设置 窗口无边框和背景透明 *必须 self.setWindowOpacity(0.9) # 设置窗口透明度 self.setAttribute(Qt.WA_TranslucentBackground) self.setWindowFlags(QtCore.Qt.FramelessWindowHint) def paintEvent(self, event): # 阴影 path = QPainterPath() path.setFillRule(Qt.WindingFill) pat = QPainter(self) pat.setRenderHint(pat.Antialiasing) pat.fillPath(path, QBrush(Qt.white)) color = QColor(192, 192, 192, 50) for i in range(10): i_path = QPainterPath() i_path.setFillRule(Qt.WindingFill) ref = QRectF(10 - i, 10 - i, self.width() - (10 - i) * 2, self.height() - (10 - i) * 2) # i_path.addRect(ref) i_path.addRoundedRect(ref, self.border_width, self.border_width) color.setAlpha(int(150 - i ** 0.5 * 50)) pat.setPen(color) pat.drawPath(i_path) # 圆角 pat2 = QPainter(self) pat2.setRenderHint(pat2.Antialiasing) # 抗锯齿 pat2.setBrush(Qt.cyan) pat2.setPen(Qt.transparent) rect = self.rect() rect.setLeft(-1) rect.setTop(-1) rect.setWidth(rect.width() - 1 ) rect.setHeight(rect.height() -1 ) pat2.drawRoundedRect(rect, 8, 8) def mousePressEvent(self, event): if event.button() == Qt.LeftButton: self.m_flag = True self.m_Position = event.globalPos() - self.pos() # 获取鼠标相对窗口的位置 event.accept() self.setCursor(QCursor(Qt.OpenHandCursor)) # 更改鼠标图标 def mouseMoveEvent(self, QMouseEvent): if Qt.LeftButton and self.m_flag: self.move(QMouseEvent.globalPos() - self.m_Position) # 更改窗口位置 QMouseEvent.accept() def mouseReleaseEvent(self, QMouseEvent): self.m_flag = False self.setCursor(QCursor(Qt.ArrowCursor)) class Ui_photo2ico(RoundShadow,QWidget): def __init__(self): super(Ui_photo2ico, self).__init__() self.setupUi() username = os.getenv('USERNAME') print(username) self.dest_path = "C:\\Users\\" + username + "\\Desktop\\" def setupUi(self): self.setObjectName("photo2ico") self.setFixedSize(458, 42) self.textEdit = QtWidgets.QTextEdit(self) self.textEdit.setGeometry(QtCore.QRect(70, 5, 221, 30)) self.textEdit.setObjectName("textEdit") self.textEdit.acceptDrops() #self.textEdit.set self.label = QtWidgets.QLabel(self) self.label.setGeometry(QtCore.QRect(11, 11, 65, 19)) font = QtGui.QFont() font.setFamily("微软雅黑") font.setPointSize(10) font.setBold(False) font.setWeight(50) self.label.setFont(font) self.label.setObjectName("label") self.comboBox = QtWidgets.QComboBox(self) self.comboBox.setGeometry(QtCore.QRect(300, 5, 81, 30)) font = QtGui.QFont() font.setFamily("微软雅黑") font.setPointSize(10) self.comboBox.setFont(font) self.comboBox.setObjectName("comboBox") self.comboBox.addItem("") self.comboBox.addItem("") self.comboBox.addItem("") self.comboBox.addItem("") self.comboBox.addItem("") self.comboBox.setStyleSheet("QComboBox {" "combobox-popup: 0;\n" # 滚动条设置必需 "border-style:none; " "padding-left:0px; " # 字体距离左边的距离 "width:80px; " "height:20px; " "font-size:14px; " "font-family:微软雅黑; " "font-weight:400; " "color:rgba(93,169,255,1);\n" "line-height:20px; }\n" "QComboBox:drop-down {" # 选择箭头样式 "width:10px; " "height:80px; " "border: none; " "subcontrol-position: right center; " # 位置 "subcontrol-origin: padding;}\n" # 对齐方式 "QComboBox:down-arrow {" # 选择箭头,继承drop-down "border: none; " "background: transparent; " "QComboBox QAbstractItemView {" # 下拉选项样式 "color:black; " "background: transparent; " "selection-color:rgba(93,169,255,1);" "selection-background-color: rgba(255,255,255,1);" "}\n" "QComboBox QAbstractScrollArea QScrollBar:vertical {" # 滚动条样式 "width: 6px;\n" "height: 100px;" "background-color: transparent; }\n" "QComboBox QAbstractScrollArea QScrollBar::handle:vertical {\n" # 滚动条样式 "border-radius: 3px; " "background: rgba(0,0,0,0);}\n" # "QComboBox QAbstractScrollArea QScrollBar::handle:vertical:hover {\n" # 划过滚动条,变化 # "background: rgb(90, 91, 93);}\n" "QComboBox QScrollBar::add-line::vertical{" # 滚动条上箭头 "border:none;}" "QComboBox QScrollBar::sub-line::vertical{" # 滚动条下箭头 "border:none;}" "") self.pushButton = QtWidgets.QPushButton(self) self.pushButton.setGeometry(QtCore.QRect(390, 5, 30, 30)) self.pushButton.setObjectName("pushButton") self.pushButton.setStyleSheet(button_style) self.pushButton_2 = QtWidgets.QPushButton(self) self.pushButton_2.setGeometry(QtCore.QRect(420, 5, 30, 30)) self.pushButton_2.setObjectName("pushButton_2") self.pushButton_2.setStyleSheet(button_style) self.retranslateUi(self) QtCore.QMetaObject.connectSlotsByName(self) self.pushButton.clicked.connect(self.toico) self.pushButton_2.clicked.connect(self.exitapp) self.textEdit.textChanged.connect(self.choosepath) def retranslateUi(self, photo2ico): _translate = QtCore.QCoreApplication.translate photo2ico.setWindowTitle(_translate("photo2ico", "photo2ico")) self.label.setText(_translate("photo2ico", "图片路径:")) self.comboBox.setItemText(0, _translate("photo2ico", "64X64")) self.comboBox.setItemText(1, _translate("photo2ico", "256X256")) self.comboBox.setItemText(2, _translate("photo2ico", "128X128")) self.comboBox.setItemText(3, _translate("photo2ico", "48X48")) self.comboBox.setItemText(4, _translate("photo2ico", "32X32")) self.pushButton.setText(_translate("photo2ico", chr(9655))) self.pushButton_2.setText(_translate("photo2ico", chr(9633))) def messageBox(self, title, text): messagebox = QMessageBox() messagebox.setWindowIcon(QIcon('cip.ico')) messagebox.setWindowTitle(title) messagebox.setText(text) messagebox.addButton(QtWidgets.QPushButton('确定'), QMessageBox.YesRole) messagebox.exec_() def choosepath(self): if 0 == self.textEdit.toPlainText().find('file:///'): self.textEdit.setText(self.textEdit.toPlainText().replace('file:///', '')) def exitapp(self): sys.exit(0) def toico(self): img_path = self.textEdit.toPlainText() if img_path != "": try: img_size = self.comboBox.currentText() img = PythonMagick.Image(img_path) img.sample(img_size) img.write(self.dest_path + 'result_' + img_size + '.ico') self.messageBox('提示',"对应的图标文件已经生成在您的桌面。") except: self.messageBox("错误","您所选择的文件不是图片格式。") else: self.messageBox('提示','请拖动图片到输入框.') if __name__ == "__main__": app = QApplication(sys.argv) form = QWidget() w = Ui_photo2ico() w.show() sys.exit(app.exec_())
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论