前言
关于本作品的声明
仅供学习和交流!!!
本项目仅供参考以及学习交流,请勿用作非法用途牟利
完整项目代码+文件可在文末获取
简述
必要下载的文件会在下方步骤中给出链接,不必要下载的文件(代码文件)也可以自行编写,当然了,如果你很懒,文末可以直接下载整个项目!
呃呃呃没什么简述,有的下载链接被和谐了,请使用文末下载链接
构建Gui窗口
简单了解PyQt5
PyQt5是Python中的一个制作Gui页面的库 ,而PyQt5-tools是一些关于pyqt5的工具 ,不过它也作为一个库,需要单独安装。
由于pip默认原网络较差,我们可以切换国内镜像源来安装(2个库分别装):
pip install PyQt5 -i https://pypi.douban.com/simple
pip install PyQt5-tools -i https://pypi.douban.com/simple
使用designer工具构建窗口
首先打开python所在的文件夹,然后依次打开
1.新建一个Widget窗口
接下来需要插入各种控件,下方是具体教程
2.加入各种控件
我们在左侧控件面板中找到图中的的各种控件,并按顺序摆好,并改为如图所示的控件名称(注意:控件名称必须与图中相符!下方写代码要调用!):
Label、PushButton控件显示的文字直接双击 窗口中的对应控件 更改即可
TextBrowser控件在 第4步 再编辑!
3.更改窗口显示名
如图所示(标题旁的window.ui不用管,python运行时会消失!)
4.更改TextBrowers中的内容
直接双击 该控件 打开编辑模式,自行编辑即可
5.存为ui文件
file—>Save As—>指定项目文件夹—>文件名:window.ui!!
养成好习惯:做项目时建一个专属文件夹,文件放一起!
ui转py
(这是pyqt5中ui转py的方法)
这里新建一个bat文件,文件名ui转py.bat
(不会新建的查阅相关资料),输入:
@echo off
pyuic5 -o window.py window.ui
双击运行!然后就会出现一个py文件:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'window.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(555, 180)
self.textBrowser = QtWidgets.QTextBrowser(Form)
self.textBrowser.setGeometry(QtCore.QRect(11, 17, 257, 145))
self.textBrowser.setObjectName("textBrowser")
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(275, 17, 68, 23))
font = QtGui.QFont()
font.setFamily("微软雅黑")
font.setPointSize(10)
self.label.setFont(font)
self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(350, 20, 181, 24))
self.lineEdit.setText("")
self.lineEdit.setObjectName("lineEdit")
self.label_2 = QtWidgets.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(275, 54, 102, 23))
font = QtGui.QFont()
font.setFamily("微软雅黑")
font.setPointSize(10)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.lineEdit_2 = QtWidgets.QLineEdit(Form)
self.lineEdit_2.setGeometry(QtCore.QRect(384, 54, 151, 24))
self.lineEdit_2.setText("")
self.lineEdit_2.setObjectName("lineEdit_2")
self.label_3 = QtWidgets.QLabel(Form)
self.label_3.setGeometry(QtCore.QRect(275, 91, 102, 23))
font = QtGui.QFont()
font.setFamily("微软雅黑")
font.setPointSize(10)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.lineEdit_3 = QtWidgets.QLineEdit(Form)
self.lineEdit_3.setGeometry(QtCore.QRect(384, 91, 151, 24))
self.lineEdit_3.setText("")
self.lineEdit_3.setObjectName("lineEdit_3")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(275, 128, 261, 34))
font = QtGui.QFont()
font.setFamily("微软雅黑")
font.setPointSize(11)
font.setBold(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "行程卡生成器V1.0"))
self.textBrowser.setHtml(_translate("Form", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'SimSun\'; font-size:7.2pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:6pt; font-weight:600; color:#ff0000;\"> </span><span style=\" font-size:18pt; font-weight:600; color:#ff0000;\">行程卡生成器</span></p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:10pt; font-weight:600; color:#000000;\"> </span><span style=\" font-size:14pt; font-weight:600; color:#000000;\">作者:lzj</span></p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:9pt; font-weight:600; color:#000000;\"> 生成的文件放在</span></p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:11pt; font-weight:600; color:#00aa00;\"> C盘-行程卡文件夹中</span></p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:9pt; font-weight:600; color:#0055ff;\"> -本程序基于python3编写-</span></p></body></html>"))
self.label.setText(_translate("Form", "所在地区"))
self.label_2.setText(_translate("Form", "手机号前三位"))
self.label_3.setText(_translate("Form", "手机号后四位"))
self.pushButton.setText(_translate("Form", "生成行程码"))
下载ui与py文件
链接:https://pan.baidu.com/s/1hA4TVCNinonvKDjhLIuQFw
提取码:0000
编写程序
逻辑概述
我们要开始写代码啦!
这个生成器的逻辑是这样的:[ps:应该可以看懂吧(ノ ̄▽ ̄)]
添加需要的文件
我们这个项目需要很多文件,如图所示,完整项目代码+文件可在文末获取
背景图片
我们可以先打开微信行程卡小程序,截一张图,如下:
然后用Photoshop(截图软件也可)用白色实体正方形盖住框选部分,如图:
下载a.png:
链接:https://pan.baidu.com/s/11lfUugWU0gmJZppnP-CS2Q
提取码:0000
字体文件
a.ttf下载
链接:https://pan.baidu.com/s/13aZyrjdvY1LD5kOCV7umHA
提取码:0000
a_bold.ttf下载
链接:https://pan.baidu.com/s/1kU8k323w4LCeYrWgPY_4_w
提取码:0000
图标图片
我们可以用ps画个图,做一下图标,并把圆外的部分扣掉
再转成ico(可以自行搜索转换网站)
下载图片与图标
链接:https://pan.baidu.com/s/16gWgH2Q8f9HsKC1D2Qv20Q
提取码:0000
提示窗口
我们可以利用windows的VBS窗口来提示
我们新建一个记事本文件(tip.txt),输入:
msgbox"您的行程卡生成成功,请至'C盘-行程卡'文件夹查看"
然后另存为一个vbs文件(tip.vbs):
现在我们运行一下tip.vbs,效果图:
下载vbs文件
链接:https://pan.baidu.com/s/1_neEmCprjkL0_3N_hMaifQ
提取码:0000
编写main.py
我们按步骤来,一步步编写(如果不想一个个复制:完整代码在本章末尾)
1.导入所需库
先来看一下都需要什么库:
请使用pycham终端或者cmd装库
需要的库的名称 | 在本程序中的作用 | 装载方法 |
---|---|---|
PIL(python3)即pillow(python2) | 图片处理 | pip install pillow |
datetime | 获取时间戳 | pip install datetime |
os | 执行系统命令 | 无需安装 |
functools | 函数便捷操作 | 无需安装 |
Pyqt5 | gui使用 | 前面已经安装 |
sys | 为gui使用辅助 | 无需安装 |
安装完成后写代码导入库
from PIL import Image,ImageFont,ImageDraw
import datetime
import os
import window #这个是刚刚生成的window.py
from functools import partial
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
2.定义“新建文件夹”函数
我们要在C盘目录下新建一个文件夹,在这里建一个函数备用
def mkdir(path):
path=path.strip() #清除空格
path=path.rstrip("\\") #删除末尾的\\
os.makedirs(path) #新建文件夹
3.定义“生成行程卡”函数
3.1.获取输入的信息
我们可以利用下方的这个命令来获取QlineEdit中输入的内容
变量 = ui.QlineEdit控件名称.text()
下面回到qt编辑器记住控件名:
敲代码:
city=ui.lineEdit.text() #获取城市
sjh_front=ui.lineEdit_2.text() #获取手机号前3位
sjh_end=ui.lineEdit_3.text() #获取手机号后4位
sjh=sjh_front+"****"+sjh_end #合并手机号
3.2.图像处理准备
我们在这里要用python打开原图和字体,后面备用
img=Image.open("a.png") #背景
font_bold=ImageFont.truetype("a_bold.ttf",13) #粗体
font=ImageFont.truetype("a.ttf",10) #细体
font_two=ImageFont.truetype("a_bold.ttf",11) #正常体
draw=ImageDraw.Draw(img) #添加到作画板
3.3.获取时间戳
这里就利用下datetime库的now,获取下当前时间(即电脑上的系统时间)
#这个是在图片上添加的文本
time=(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
#这个是“C:/行程卡”下生成的图片文件名
time_path=(datetime.datetime.now().strftime('%Y{y}%m{m}%d{d}%H{h}%M{mw}%S{s}').format(y='年', m='月', d='日', h='时', mw='分', s="秒"))
关于%Y
、%m
、%d
、%H
等格式符,请看下方(格式符区分大小写!)
格式符 | 说明 |
---|---|
%a | 星期的英文单词的缩写:如星期一, 则返回 Mon |
%A | 星期的英文单词的全拼:如星期一,返回 Monday |
%b | 月份的英文单词的缩写:如一月, 则返回 Jan |
%B | 月份的引文单词的缩写:如一月, 则返回 January |
%c | 返回datetime的字符串表示,如03/08/15 23:01:26 |
%d | 返回的是当前时间是当前月的第几天 |
%f | 微秒的表示: 范围: [0,999999] |
%H | 以24小时制表示当前小时 |
%I | 以12小时制表示当前小时 |
%j | 返回 当天是当年的第几天 范围[001,366] |
%m | 返回月份 范围[0,12] |
%M | 返回分钟数 范围 [0,59] |
%P | 返回是上午还是下午–AM or PM |
%S | 返回秒数 范围 [0,61]。。。手册说明的 |
%U | 返回当周是当年的第几周 以周日为第一天 |
%W | 返回当周是当年的第几周 以周一为第一天 |
%w | 当天在当周的天数,范围为[0, 6],6表示星期天 |
%x | 日期的字符串表示 :03/08/15 |
%X | 时间的字符串表示 :23:22:08 |
%y | 两个数字表示的年份 15 |
%Y | 四个数字表示的年份 2015 |
%z | 与utc时间的间隔 (如果是本地时间,返回空字符串) |
%Z | 时区名称(如果是本地时间,返回空字符串) |
3.4.在图片上添加字符
我们现在往图片上添加字符,可以利用下面的方法
画板.text((位置x,位置y),内容,fill=(颜色R,颜色G,颜色B),font=字体)
敲代码:
#写入手机号
draw.text((88,188),sjh,fill=(95,95,95),font=font_bold)
#写入时间戳
draw.text((138,220),time,fill=(155,155,155),font=font)
#写入城市
draw.text((181,434),city,fill=(70,70,70),font=font_two)
3.5.保存到本地并提示
#保存图片到本地
path=r"C:/行程卡/"+time_path+".jpg" #设置路径
img.save(path)
#生成完毕提示
os.system("@echo off") #cmd终端不提示
os.system("start tip.vbs") #打开tip.vbs提示窗
这里的os.system(内容)
的作用是在cmd终端运行内容
3.finish.完整代码
下面是上方 3.1至3.5代码+def 的整合
def convert(ui):
#获取地点以及手机号
city=ui.lineEdit.text()
sjh_front=ui.lineEdit_2.text()
sjh_end=ui.lineEdit_3.text()
sjh=sjh_front+"****"+sjh_end
#打开图片和字体
img=Image.open("a.png")
font_bold=ImageFont.truetype("a_bold.ttf",13)
font=ImageFont.truetype("a.ttf",10)
font_two=ImageFont.truetype("a_bold.ttf",11)
draw=ImageDraw.Draw(img)
#获取时间戳
time=(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
time_path=(datetime.datetime.now().strftime('%Y{y}%m{m}%d{d}%H{h}%M{mw}%S{s}').format(y='年', m='月', d='日', h='时', mw='分', s="秒"))
#写入手机号
draw.text((88,188),sjh,fill=(95,95,95),font=font_bold)
#写入时间戳
draw.text((138,220),time,fill=(155,155,155),font=font)
#写入城市
draw.text((181,434),city,fill=(70,70,70),font=font_two)
#保存图片到本地
path=r"C:/行程卡/"+time_path+".jpg"
img.save(path)
#生成完毕提示
os.system("@echo off")
os.system("start tip.vbs")
4.运行程序
这部分就直接上代码啦!
if __name__ == '__main__':
#尝试新建文件夹(已经存在则except,不存在则执行mkdir函数)
try:
mkdir("c:\\行程卡\\")
except Exception:
print()
#打开gui页面
app = QApplication(sys.argv)
mainWindow = QMainWindow()
ui = window.Ui_Form()
ui.setupUi(mainWindow)
mainWindow.show()
#按下按钮执行convert函数
ui.pushButton.clicked.connect(partial(convert,ui))
#关闭窗口退出
sys.exit(app.exec_())
完整代码
#导入库
from PIL import Image,ImageFont,ImageDraw
import datetime
import os
import window
from functools import partial
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
#新建文件夹定义
def mkdir(path):
path=path.strip()
path=path.rstrip("\\")
os.makedirs(path)
#定义按下按钮
def convert(ui):
#获取地点以及手机号
city=ui.lineEdit.text()
sjh_front=ui.lineEdit_2.text()
sjh_end=ui.lineEdit_3.text()
sjh=sjh_front+"****"+sjh_end
#打开图片和字体
img=Image.open("a.png")
font_bold=ImageFont.truetype("a_bold.ttf",13)
font=ImageFont.truetype("a.ttf",10)
font_two=ImageFont.truetype("a_bold.ttf",11)
draw=ImageDraw.Draw(img)
#获取时间戳
time=(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
time_path=(datetime.datetime.now().strftime('%Y{y}%m{m}%d{d}%H{h}%M{mw}%S{s}').format(y='年', m='月', d='日', h='时', mw='分', s="秒"))
#写入手机号
draw.text((88,188),sjh,fill=(95,95,95),font=font_bold)
#写入时间戳
draw.text((138,220),time,fill=(155,155,155),font=font)
#写入城市
draw.text((181,434),city,fill=(70,70,70),font=font_two)
#保存图片到本地
path=r"C:/行程卡/"+time_path+".jpg"
img.save(path)
#生成完毕提示
os.system("@echo off")
os.system("start tip.vbs")
#运行
if __name__ == '__main__':
try:
mkdir("c:\\行程卡\\")
except Exception:
print()
app = QApplication(sys.argv)
mainWindow = QMainWindow()
ui = window.Ui_Form()
ui.setupUi(mainWindow)
mainWindow.show()
ui.pushButton.clicked.connect(partial(convert,ui))
sys.exit(app.exec_())
下载main.py:
链接:https://pan.baidu.com/s/1oLQ6pPj8lBzL5P52vcgs4A
提取码:0000
文件打包exe
安装pyinstaller库
直接打开cmd或pycharm终端,输入并回车运行
pip install pyinstaller
新建bat文件
新建一个bat文件,文件名:打包.bat
,输入以下代码并保存:
pyinstaller -w -i icon.ico -F main.py
set /p exit=回车退出
运行bat文件
双击运行即可,出现下方内容为成功 (你的python为几位的,打包出来的程序就是几位的,可以用虚拟机装位数不同于实体机的python与pyinstaller,以便制作不同位的exe)
然后打开 项目文件夹–dist,里面的exe就是打包好的文件了!
记得把它拖动到项目文件夹下,在dist运行会找不到字体、背景图片文件!
拖动后再改个名字~完美!
下载exe文件(x32 and x64)
链接:https://pan.baidu.com/s/1TYDPgKYCkWuYSVrQYSqYog
提取码:0000
项目下载和试用
下载整个项目:点我(提取码:0000)
我们来生成一个行程卡:
生成的结果:
感谢浏览!