将 JSON 文件转换为二维码时出错

Error to convert JSON file into a QR-Code

我正在尝试使用 pyqrcode

将 json 文件转换为二维码
import tkinter as tk
from tkinter import filedialog
import json
import pyqrcode as qrcode

root= tk.Tk()
root.title('Convertitore CSV in JSON')

canvas1 = tk.Canvas(root, width = 300, height = 300, bg = 'lightsteelblue2', relief = 'raised')
canvas1.pack()

label1 = tk.Label(root, text='Tool Conversione File', bg = 'lightsteelblue2')
label1.config(font=('helvetica', 20))
canvas1.create_window(150, 60, window=label1)

def json_to_qrcode(jsonFilePath2, imageFilePath):
    with open(jsonFilePath2, 'w', encoding='utf-8') as jsonf:
        jsonReader = json.load(jsonf)
         
        qr = qrcode.QRCode(version = 1, box_size = 15, border = 5)
        data = jsonReader
        qr.add_data(data)
        qr.make(fit = True)
        img = qr.make_image(fill = 'black', back_color = 'white')
        img.save('Dieta.png')
        
jsonFilePath2 = filedialog.askopenfilename()
imageFilePath = filedialog.asksaveasfilename(mode='w', defaultextension=".png")

browseButton_CSV = tk.Button(text="     Importa JSON File     ", command=json_to_qrcode(jsonFilePath2, imageFilePath), bg='green', fg='white', font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 130, window=browseButton_CSV)

root.mainloop()

这里是 JSON 文件的示例:

[
    {
        "Giorno": "Lunedi",
        "Tipologia": "Prima Colazione",
        "Alimento": "Fette biscottate",
        "Quantita": "3",
        "UDM": "fette",
        "Note": ""
    },
    {
        "Giorno": "Lunedi",
        "Tipologia": "Prima Colazione",
        "Alimento": "Latte",
        "Quantita": "200",
        "UDM": "g",
        "Note": ""
    }
]

当我尝试 运行 代码时,它给我这个错误:“TclError:错误的选项“-mode”:必须是 -confirmoverwrite,-defaultextension,-filetypes,-initialdir,-initialfile, -parent、-title 或 -typevariable

我该如何解决? 提前致谢。

你能试试下面的代码吗?我想你的问题现在应该已经解决了。

请安装枕头库。 (pip 安装枕头)

问题

  1. 您正试图为 filedialog.asksaveasfilename() 函数传递错误的参数。

  2. 您正在使用 pyqrcode 而不是 qrcode。 (您已经编写了有关二维码库的代码)

     import tkinter as tk
     from tkinter import filedialog
     import json
     import qrcode
    
     root = tk.Tk()
     root.title('Convertitore CSV in JSON')
    
     canvas1 = tk.Canvas(root, width=300, height=300, bg='lightsteelblue2', relief='raised')
     canvas1.pack()
    
     label1 = tk.Label(root, text='Tool Conversione File', bg='lightsteelblue2')
     label1.config(font=('helvetica', 20))
     canvas1.create_window(150, 60, window=label1)
    
    
     def json_to_qrcode(jsonFilePath2, imageFilePath):
         with open(jsonFilePath2, 'r') as jsonf:
           jsonReader = json.load(jsonf)
    
           qr = qrcode.QRCode(version=1, box_size = 15, border = 5)
           data = jsonReader
           qr.add_data(data)
           qr.make(fit=True)
           img = qr.make_image(fill='black', back_color='white')
           img.save('Dieta.png')
    
    
    jsonFilePath2 = filedialog.askopenfilename()
    imageFilePath = filedialog.asksaveasfilename()
    
    browseButton_CSV = tk.Button(text="     Importa JSON File     ", 
    command=json_to_qrcode(jsonFilePath2, imageFilePath),
                              bg='green', fg='white', font=('helvetica', 12, 'bold'))
    canvas1.create_window(150, 130, window=browseButton_CSV)
    
    root.mainloop()