I'm making a game and I started working on the save and loading part it's supposed to change some text based on a value in the JSON file and when I try to import the JSON file by giu that I built I get this error
Traceback (most recent call last):
File "save_test.py", line 112, in <module>
object.process()
File "save_test.py", line 65, in process
self.onclickFunction()
File "save_test.py", line 83, in open_file
import_text()
File "save_test.py", line 91, in import_text
importedText = data_load(['Text']['text'])
TypeError: list indices must be integers or slices, not str
you will need a JSON file so that you can import it then you run the code
Code:
# Imports
import sys
import pygame
import tkinter as tk
from tkinter import filedialog
import json
root = tk.Tk()
root.withdraw()
# Configuration
pygame.init()
fps = 60
fpsClock = pygame.time.Clock()
width, height = 640, 480
screen = pygame.display.set_mode((width, height))
white = (255, 255, 255)
green = (0, 255, 0)
blue = (0, 0, 128)
font = pygame.font.SysFont('Arial', 40)
objects = []
importedText = "example text"
class Button():
def __init__(self, x, y, width, height, buttonText='Button', onclickFunction=None, onePress=False):
self.x = x
self.y = y
self.width = width
self.height = height
self.onclickFunction = onclickFunction
self.onePress = onePress
self.fillColors = {
'normal': '#ffffff',
'hover': '#666666',
'pressed': '#333333',
}
self.buttonSurface = pygame.Surface((self.width, self.height))
self.buttonRect = pygame.Rect(self.x, self.y, self.width, self.height)
self.buttonSurf = font.render(buttonText, True, (20, 20, 20))
self.alreadyPressed = False
objects.append(self)
def process(self):
mousePos = pygame.mouse.get_pos()
self.buttonSurface.fill(self.fillColors['normal'])
if self.buttonRect.collidepoint(mousePos):
self.buttonSurface.fill(self.fillColors['hover'])
if pygame.mouse.get_pressed(num_buttons=3)[0]:
self.buttonSurface.fill(self.fillColors['pressed'])
if self.onePress:
self.onclickFunction()
elif not self.alreadyPressed:
self.onclickFunction()
self.alreadyPressed = True
else:
self.alreadyPressed = False
self.buttonSurface.blit(self.buttonSurf, [
self.buttonRect.width/2 - self.buttonSurf.get_rect().width/2,
self.buttonRect.height/2 - self.buttonSurf.get_rect().height/2
])
screen.blit(self.buttonSurface, self.buttonRect)
def open_file():
global importedText
importedText = filedialog.askopenfilename(filetypes=(("Lil brute save file", "*.json"), ("All Files", "*.*")))
import_text()
def import_text():
global importedText
f = open(importedText)
data_load = json.load(f)
importedText = data_load(['Text']['text'])
# set the center of the rectangular object.
customButton = Button(30, 30, 400, 100, 'import',open_file)
# Game loop.
while True:
screen.fill((20, 20, 20))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
text = font.render(importedText, True, green, blue)
# create a rectangular object for the
# text surface object
textRect = text.get_rect()
textRect.center = (width // 2, height // 2)
for object in objects:
object.process()
screen.blit(text, textRect)
pygame.display.flip()
pygame.display.update()
fpsClock.tick(fps)
JSON file: {"Text": {"text": "import successful"}}
CodePudding user response:
Your code is calling data_load as a function, but it is a dictionary. Change your import_text function to:
def import_text():
global importedText
f = open(importedText)
data_load = json.load(f)
importedText = data_load['Text']['text']