简体   繁体   English

python json 列表索引必须是整数或切片,而不是 str 错误

[英]python json list indices must be integers or slices, not str error

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我正在制作游戏,我开始处理保存和加载部分,它应该根据 JSON 文件中的值更改一些文本,当我尝试导入我构建的 giu 的 JSON 文件时,我收到此错误


    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您将需要一个 JSON 文件,以便您可以导入它然后运行代码
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"}} JSON 文件: {"Text": {"text": "import successful"}}

Your code is calling data_load as a function, but it is a dictionary.您的代码将 data_load 作为 function 调用,但它是一个字典。 Change your import_text function to:将您的 import_text function 更改为:

def import_text():
    global importedText
    f = open(importedText)
    data_load = json.load(f)
    importedText = data_load['Text']['text']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Python TypeError 解析 JSON:列表索引必须是整数或切片,而不是 str - Parsing JSON with Python TypeError: list indices must be integers or slices, not str Python JSON TypeError 列表索引必须是整数或切片,而不是 str - Python JSON TypeError list indices must be integers or slices, not str Python JSON 类型错误:列表索引必须是整数或切片,而不是字符串 - Python JSON TypeError: list indices must be integers or slices, not str Python JSON 类型错误:列表索引必须是整数或切片,而不是字符串 - Python JSON TypeError : list indices must be integers or slices, not str Python / JSON - 类型错误:列表索引必须是整数或切片,而不是 str - Python / JSON - TypeError: list indices must be integers or slices, not str 列表索引必须是整数或切片而不是 str python - list indices must be integers or slices not str python Python 列表索引必须是整数或切片,而不是 str - Python List indices must be integers or slices, not str 错误列表索引必须是整数或切片,而不是str - error list indices must be integers or slices, not str 列表索引必须是整数或切片,而不是 str 错误 Python - list indices must be integers or slices, not str error Python Python 错误:列表索引必须是整数或切片,而不是 str - Python Error : list indices must be integers or slices, not str
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM