我目前正在开发一个小型数据库,可以在其中放置具有不同属性的植物。为了方便查找植物,我计划生成二维码以贴在花盆上。我想创建一个 QR 码图像,其内容位于下方(在本例中为植物 ID 号)。有办法做到这一点吗?
我的数据库使用 Tkinter 和 SQlite3。这是我到目前为止得到的代码。它生成一个没有文本的普通二维码:
import qrcode
def qr():
selected_item = tree.focus()
if not selected_item:
messagebox.showerror("Error", "Choose a plant to generate a qr-code for")
else:
qr = plant_id_entry.get()
img = qrcode.make(qr)
type(img)
img.save(f"{qr} {date.today()}.png")
messagebox.showinfo("Success", "A qr-code has been generated")
我尝试用谷歌搜索这个问题,但似乎没有弹出任何内容,只有有关二维码中徽标的建议。
您可以使用Spire.Barcode for Python模块生成带有文本的二维码图像。
pip install Spire.Barcode
这是一个代码示例:
from spire.barcode import *
# Create a BarcodeSettings object
barcodeSettings = BarcodeSettings()
# Set the barcode type as QR code
barcodeSettings.Type = BarCodeType.QRCode
# Set the data of the QR code
barcodeSettings.Data = "12345ABCDE"
barcodeSettings.Data2D = "12345ABCDE"
# Set the width of the QR code bar module
barcodeSettings.X = 3
# Set the error correction level of the QR code
barcodeSettings.QRCodeECL = QRCodeECL.M
# Set the top and bottom text
barcodeSettings.TopText = "Top Text"
barcodeSettings.TopTextColor = Color.get_Blue()
barcodeSettings.BottomText = "Bottom Text"
barcodeSettings.BottomTextColor = Color.get_Blue()
# Set text visibility
barcodeSettings.ShowText = False
barcodeSettings.ShowTopText = True
barcodeSettings.ShowBottomText = True
# Create a BarCodeGenerator object with the specified settings
barCodeGenerator = BarCodeGenerator(barcodeSettings)
# Generate QR code image
barcodeimage = barCodeGenerator.GenerateImage()
# Save the QR code image to a .png file
with open("QRCodeWithText.png", "wb") as file:
file.write(barcodeimage)
结果如下:
更多详情,您可以查看此博客:https://medium.com/@alice.yang_10652/how-to-generate-qr-codes-in-python-a-compressive-guide-8e1495d6a12c#4572
注:我在开发该模块的公司工作。