-2

I'm making a pocket guide app for Stardew Valley as my first project. Everything worked perfectly until I added the part, where the code reads information of the database.txt file. I have no idea, what I can even try. Here's the error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\danib\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "C:/Users/danib/PycharmProjects/StardewApp/Main.py", line 42, in <lambda>
    searchBarEntry.bind("<Return>", lambda event: (search(searchBarEntry.get()), searchBarEntry.delete(0, "end")))
  File "C:/Users/danib/PycharmProjects/StardewApp/Main.py", line 13, in search
    dict_data = json.loads("{" + the_data + "}")
  File "C:\Users\danib\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "C:\Users\danib\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\danib\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 11 (char 10)

Process finished with exit code 0

Here's my code:

import tkinter as tk
import json


def search(entry):
    print("This is the entry: " + entry)

    entry = entry.lower()
    entry = entry.replace(" ", "")

    data_read = open("database.txt", "r")
    the_data = data_read.read()
    dict_data = json.loads("{" + the_data + "}")
    data_read.close()

    try:
        infoLabel[dict_data[entry]]

    except KeyError:
        data_append = open("database.txt", "a")
        infoLabel["text"] = "No data, please give some."
        item_info = input()
        data_append.write(", \n\"" + entry + "\": " + "\"\"\"" + item_info + "\"\"\"")
        data_append.close()


root = tk.Tk()
root.geometry("720x480")

bgImg = tk.PhotoImage(file="bg.png")
bgLabel = tk.Label(root, image=bgImg)
bgLabel.place(relwidth=1, relheight=1)
bt1Img = tk.PhotoImage(file="icon.png")
defBtImg = tk.PhotoImage(file="define.png")

searchBarFrame = tk.Frame(root, bd=5, bg="gray50")
searchBarFrame.place(relwidth=0.92, relheight=0.1,
                     relx=0.04, rely=0.08)

searchBarEntry = tk.Entry(searchBarFrame, font=50, bd=0,
                          bg="gray55", fg="gray15")
searchBarEntry.bind("<Return>", lambda event: (search(searchBarEntry.get()), searchBarEntry.delete(0, "end")))
searchBarEntry.place(relwidth=1, relheight=1)

infoFrame = tk.Frame(root, bd=5, bg="gray50")
infoFrame.place(relwidth=0.7, relheight=0.72,
                relx=0.04, rely=0.20)

infoLabel = tk.Label(infoFrame, bg="gray55", justify="left", font=("courier", "12"), anchor="nw")
infoLabel.place(relwidth=1, relheight=1)

sideFrame = tk.Frame(root, bd=5, bg="gray50")
sideFrame.place(relwidth=0.2, relheight=0.72,
                relx=0.76, rely=0.20)

sideButton1 = tk.Button(sideFrame, bg="gray55", bd=0, image=bt1Img)
sideButton1.place(relwidth=1, relheight=0.2)

root.mainloop()

Here's also the text file containing the information.

"axe": """Axe

The Axe is the axe you start with. Nothing special.

How many chops to break:
Tree        - 10
Stump       - 5
Large Stump - Incapable
Large Log   - Incapable"""
PoolersJr
  • 1
  • 1

1 Answers1

1

You cannot use multiline strings in json. use \n and \t to format your data. And if its json data, why save as txt and pre-postfix {}?

Multiline strings in JSON

Do something like this:


{
    "axe": "Axe\n\nThe Axe is the axe you start with. Nothing special.\n\nHow many chops to break:\nTree        - 10\nStump       - 5\nLarge Stump - Incapable\nLarge Log   - Incapable"
}

or if you really need it in your .txt file then just add to your txt file:


"axe": "Axe\n\nThe Axe is the axe you start with. Nothing special.\n\nHow many chops to break:\nTree        - 10\nStump       - 5\nLarge Stump - Incapable\nLarge Log   - Incapable"