各位同学,我想根据项目去展开学习! 可以将每句话都给我做个注释!
有python基础,可以看得懂!
from tkinter import *
from PIL import Image
from PIL import ImageTk
import qrcode
root = Tk()
root.title('二维码生成器') #
img = qrcode.make('https://www.codemao.cn')
img.save('./hello.png')
img = Image.open('hello.png')
img = ImageTk.PhotoImage(img)
t = Text(root, height=10, font=("黑体", 15))
t.grid(row=0, column=0, padx=10, pady=5)
def create_qrcode(text, filename):
"""
生成二维码图片
"""
qr = qrcode.QRCode(
version=None,
error_correction=qrcode.ERROR_CORRECT_H,
box_size=5,
border=1
)
qr.make(fit=True)
qr.add_data(text)
# fill_color和back_color参数改变生成图片的格子颜色和背景颜色
img = qr.make_image(fill_color="Tan", back_color="white")
img.save(filename+'.png')
def callback():
# 获取文本框内的内容
text_input = t.get(0.0, "end")
# 定义图片的名字为输入内容的第一个字符
img_name = text_input[0:1]
create_qrcode(text_input, img_name)
img = Image.open(img_name+'.png')
img = img.resize((500, 500))
img = ImageTk.PhotoImage(img)
label.configure(image=img)
label.image = img
btn = Button(root, text='点我生成二维码', width=20, font=("黑体", 30), command=callback)
btn.grid(row=1, column=0, pady=20)
label = Label(root, image=img, width=500, height=500)
label.grid(row=2, column=0)
root.mainloop()