Pyqt5+designer+python 实现个性签名小软件
最终效果图如上所示第一步
先在designer画好软件效果布局 保存为qianming2.ui 然后在vscode里转成Ui_qianming2.py文件# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'f:\pydemo\Gui_Pyqt5\qianming2.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(786, 499)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(30, 30, 321, 51))
self.textEdit.setObjectName("textEdit")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(30, 100, 141, 51))
self.pushButton.setObjectName("pushButton")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(30, 190, 661, 201))
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap("157977128237101.gif"))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(370, 60, 71, 16))
self.label_2.setObjectName("label_2")
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(450, 30, 141, 51))
self.comboBox.setLayoutDirection(QtCore.Qt.LeftToRight)
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 786, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "个性签名设计"))
self.pushButton.setText(_translate("MainWindow", "马上设计"))
self.label_2.setText(_translate("MainWindow", "请写入名字"))
self.comboBox.setItemText(0, _translate("MainWindow", "个性签"))
self.comboBox.setItemText(1, _translate("MainWindow", "连笔签"))
self.comboBox.setItemText(2, _translate("MainWindow", "潇洒签"))
self.comboBox.setItemText(3, _translate("MainWindow", "草体签"))
self.comboBox.setItemText(4, _translate("MainWindow", "合文签"))
self.comboBox.setItemText(5, _translate("MainWindow", "商务签"))
self.comboBox.setItemText(6, _translate("MainWindow", "可爱签"))
第二步
编写代码:import sys
from PyQt5.QtWidgets import QApplication, QMainWindow,QMessageBox
from PyQt5.QtGui import *
from PyQt5 import QtWidgets
import Ui_qianming2
from functools import partial
import requests
from bs4 import BeautifulSoup
#这里是消息提示弹出框的主要代码
class MyWindow(QtWidgets.QWidget):
def __init__(self):
super().__init__()
#消息提示框
def msg(self):
QMessageBox.warning(self,"提示框","输入框不能留空")
# QMessageBox.question(self,"消息框标题","这是一条问答。")
# QMessageBox.information(self,"消息框标题","这是一条消息。")
#主要处理函数
def convert(ui):
text = ui.comboBox.currentText()#返回选中项的文本内容
if text == '个性签':
font = 'jfcs.ttf'
elif text == '连笔签':
font = 'qmt.ttf'
elif text == '潇洒签':
font = 'bzcs.ttf'
elif text == '草体签':
font = 'lfc.ttf'
elif text == '合文签':
font = 'haku.ttf'
elif text == '商务签':
font = 'zql.ttf'
else:
font = 'yqk.ttf'
start_url = 'http://www.uustv.com/'
name = ui.textEdit.toPlainText()
if name == '':
myw = MyWindow()
myw.msg()
return
else:
name = ui.textEdit.toPlainText()
data = {
'word': name,
'sizes': '60',
'fonts': font,
'fontcolor': '#000000'
}
result = requests.post(start_url, data=data)
result.encoding='utf-8'
html = result.text
soup = BeautifulSoup(html,'html.parser')
img_url = soup.find('div', class_="tu").find('img')['src']
Urls = start_url + img_url
res = requests.get(Urls)
#引用网络图片地址
img = QImage.fromData(res.content)
ui.label.setPixmap(QPixmap.fromImage(img))
'''
#已用本地图片地址代码
pic=res.content
with open('{}'.format(img_url.split('/')[-1]), 'wb') as f:
f.write(pic)
ui.label.setPixmap(QPixmap(img_url.split('/')[-1]))
'''
if __name__ == '__main__':
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = Ui_qianming2.Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
ui.pushButton.clicked.connect(partial(convert, ui))
ui.comboBox.activated.connect(partial(convert, ui))
sys.exit(app.exec_())
联系站长
友情链接
其他入口
QQ与微信加好友
粤ICP备17018681号 站点地图 www.iamdu.com 版权所有 服务商提供:阿里云 Designed by :DU
本站部分资源内容来源于网络,若侵犯您的权益,请联系删除!