废话
需要成品的白嫖党可以点击这里来查看我的成品。
记得Star
正文
本Python程序使用Python 3.x
请先在cmd.exe
输入
pip3 --version
如果你未安装pip
,可以点击这里下载.py
文件并运行Python 3.4之后的版本都有pip
本代码使用
qrcode
模块,请先安装PIL
与qrcode
pip install PIL
pip install qrcode
代码
建议先看教程哦。
import qrcode
from tkinter import *
import tkinter.messagebox as messagebox
window = Tk()
window.title('生成二维码 By 泠风寒声')
width = 300
heigh = 150
screenwidth = window.winfo_screenwidth()
screenheight = window.winfo_screenheight()
window.geometry('%dx%d+%d+%d'%(width, heigh, (screenwidth-width)/2, (screenheight-heigh)/2))
window.resizable(0, 0)
qr = qrcode.QRCode(version=1,error_correction =
qrcode.constants.ERROR_CORRECT_M,
box_size=10, border=4,)
def generate():
img = qrcode.make(data = data)
img.show()
def closeWindow():
askback = messagebox.askyesno('提示', '真的要关闭这个程序吗?')
if askback == True:
window.destroy()
ft = ('等线Light', 10)
data = Entry(window, font = ft)
data.place(width = 150, heigh = 25, x = 75, y = 0)
button = Button(window, text = '生成二维码', font = ft,
command = generate, height = 5, width = 10)
button.place(x = 100, y = 40)
Label(window,text = 'By 泠风寒声', fg = 'red', font = ft).place(x = 225, y = 120)
window.protocol('WM_DELETE_WINDOW', closeWindow)
window.mainloop()
教程
简单版是进行命令行交互,没有GUI;加强版加入了GUI。简单版与加强版的逻辑是一样的。
简单版
代码全文
import qrcode
data = input('输入一些数据\n')
img = qrcode.make(data = data)
img.show()
import qrcode # 使用qrcode模块
使用
qrcode
模块请先安装PIL
模块与qrcode
模块
在此输入要放进二维码里的数据
data = input('输入一些数据\n')
一个十分不优雅的input
将二维码图片放进img
变量
img = qrcode.make(data = data)
显示二维码
img.show()
(会使用默认的照片打开方式打开照片)
加强版
生成一个窗口
window = Tk()
window.title('生成二维码 By 泠风寒声') # 窗口名称
window.mainloop() # 消息循环
窗口位置设置
width = 300 # 设置窗口的宽
heigh = 150 # 设置窗口的高
screenwidth = window.winfo_screenwidth()
screenheight = window.winfo_screenheight()
window.geometry('%dx%d+%d+%d'%(width, heigh, (screenwidth-width)/2, (screenheight-heigh)/2)) # 居中窗口
window.resizable(0, 0) # 禁止调整窗口大小(调整大小会引起排版错误)
定义二维码
qr = qrcode.QRCode(version=1,error_correction =
qrcode.constants.ERROR_CORRECT_M,
box_size=10, border=4,)
qrcode.QRCode
函数用法:
version
- 一个整数,范围为1到40,表示二维码的大小(最小值是1,是个12×12的矩阵),如果想让程序自动生成,将值设置为None
并使用fit=True
参数即可。error_correction
- 可以选择4个常量:ERROR_CORRECT_L 7%以下的错误会被纠正
ERROR_CORRECT_M (default) 15%以下的错误会被纠正
ERROR_CORRECT_Q 25 %以下的错误会被纠正
ERROR_CORRECT_H. 30%以下的错误会被纠正
(错误纠正率越高,二维码识别的速度就越慢,二维码也越大)
box_size
- 每个点方块中的像素个数border
- 二维码距图像外围边框距离,默认为4,最小为4。
定义generate
为生成二维码
def generate():
img = qrcode.make(data = data)
img.show()
定义closeWindow
为显示是否关闭程序的对话框
def closeWindow():
askback = messagebox.askyesno('提示', '真的要关闭这个程序吗?')
if askback == True:
window.destroy()
设置字体为等线Light
,设置字体大小为10
ft = ('等线Light', 10)
定义输入框输入的内容存放进data
变量中
data = Entry(window, font = ft)
放置输入框
data.place(width = 150, heigh = 25, x = 75, y = 0)
放置按钮,按钮显示的文字为生成二维码
,并放置按钮
button = Button(window, text = '生成二维码', font = ft,
command = generate, height = 5, width = 10)
button.place(x = 100, y = 40)
放置标语,文字为By 泠风寒声
,并放置
Label(window,text = 'By 泠风寒声', fg = 'red', font = ft).place(x = 225, y = 120)
侦测使用者是否关闭窗口,如关闭,则运行closeWindow
window.protocol('WM_DELETE_WINDOW', closeWindow)
废话
需要打包成.exe
?
安装pyinstaller
pip install pyinstaller
安装完成后,可以输入pyinstaller
查看是否安装成功。
在cmd.exe
里进入Python文件的目录
模拟一个cmd.exe
C:\Users\lfhsheng>D:
D:\>cd 你的文件夹路径
D:\lfhsheng's Program>pyinstaller -F 你的文件名.py
一番操作后,文件夹里会多出3个文件夹原有的文件不会消失,打开dist
文件夹,里面就是你刚才打包的.exe
文件
其他两个文件夹没啥用强迫症患者早就已经手痒了
总结
代码是挺简单,只不过打包后的.exe
文件就……二三十M了