-2

I'm trying to convert a json file into a qr code using pyqrcode

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()

Here is an example of JSON file:

[
    {
        "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": ""
    }
]

When I try to run the code it gives to me this error: "TclError: bad option "-mode": must be -confirmoverwrite, -defaultextension, -filetypes, -initialdir, -initialfile, -parent, -title, or -typevariable"

How can I solve it? Thanks in advance.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • Can you provide the sample program, As the snippet that you have provided is not enough. – Dhruv Rajkotia Feb 27 '21 at 17:20
  • Can you provide sample JSON file? – Dhruv Rajkotia Feb 27 '21 at 17:28
  • @DhruvRajkotia added! – user15254403 Feb 27 '21 at 17:32
  • Please provide the expected [MRE - Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). Show where the intermediate results deviate from the ones you expect. We should be able to paste a single block of your code into file, run it, and reproduce your problem. This also lets us test any suggestions in your context. [Include your minimal data frame](https://stackoverflow.com/questions/52413246/how-to-provide-a-reproducible-copy-of-your-dataframe-with-to-clipboard) as part of the example. Also provide the *entire* error message, including traceback. – Prune Feb 27 '21 at 17:41

1 Answers1

1

Can you try with the below code. I think your problem should be resolved now.

please install pillow library. (pip install pillow)

issues

  1. You are trying to pass wrong args for filedialog.asksaveasfilename() function.

  2. you are using pyqrcode instead of qrcode. (you have written code regarding qrcode library)

     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()
    
Dhruv Rajkotia
  • 370
  • 2
  • 8