1 前言
在如今信息发达的时代,二维码已经是人们生活中不可或缺的东西。比如几乎每天都要用的微信或支付宝支付。那么如何可以制作一个二维码呢?小编将在本文中给大家分享一个自制的二维码生成器。
多人学习python,不知道从何学起。
很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手。
很多已经做案例的人,却不知道如何去学习更加高深的知识。
那么针对这三类人,我给大家提供一个好的学习平台,免费领取视频教程,电子书籍,以及课程的源代码!??¤
QQ群:1057034340
2 准备
这个二维码生成器是由qrcode(生成二维码)库与 tkinter (图形 ui 界面)组成的。首先先在命令行安装以下三个模块,分别是 qrcode 、 image 、 pillow ( PIL )。安装方式很简单。
pip install qrcode
pip install image
pip install pillow
安装完整过后直接在py文件中导入以下模块和方法:
from tkinter import *
from tkinter.filedialog import *
from PIL import Image,ImageTk
import qrcode
3 具体步骤
3.1 编写 ui 界面
导入模块后直接用tkinter模块编写 ui 界面。小编这里的 ui 界面为:
图3.1ui界面
具体代码如下:
root = Tk()
root.title("二维码生成器 ")
root.geometry('600x400+400+100')
button1 = Button(root,text = '选择图标 ',font = (' 宋体 ',20),fg = 'green',bg = 'white',command = openfile)# 设置按钮
button2 = Button(root,text = '保存二维码 ',font = (' 宋体 ',20),fg = 'green',bg = 'white',command = savefile)# 设置按钮
button1.place(x = 90,y = 330,width = 120,height = 50)#显示按钮
button2.place(x = 385,y = 330,width = 150,height = 50)#显示按钮
label1 = Label(root,text = '输入链接 ',font = (' 宋体 ',20),fg = 'black',bg = 'white')# 设置组件
label1.place(x = 235,y = 5,width = 130,height = 50)
entry1 = Entry(root,font = ('宋体 ',20))# 设置输