简体   繁体   English

python 错误 - TypeError:字符串索引必须是整数

[英]python error - TypeError: string indices must be integers

I have been building a flashcard app and have run into a roadblock while trying to implement a radiobutton.我一直在构建一个抽认卡应用程序,并在尝试实现单选按钮时遇到了障碍。 The issue is when run the menu shows up and your able to access the lesson, but the radiobuttons do not appear.问题是当运行菜单显示并且您可以访问课程时,单选按钮没有出现。 Whenever the code is run this error shows up TypeError: string indices must be integers attached to the radiobutton function balancing_radio_butto1 = Radiobutton(balancing_frame, text = balancing[answer_list[0]], variable=balancing_radio, value = 1) if someone could explain the why this error shows up as well as how to fix it it would be much appreciated.每当运行代码时,都会显示此错误TypeError: string indices must be integers attach to the radiobutton function balance_radio_butto1 balancing_radio_butto1 = Radiobutton(balancing_frame, text = balancing[answer_list[0]], variable=balancing_radio, value = 1)如果有人可以解释为什么会出现此错误以及如何修复它,将不胜感激。 Below is my code that I have so far.以下是我到目前为止的代码。

from tkinter import *
from PIL import ImageTk, Image
from random import branding
import random

root = Tk()
root.title('Chemistry Flashcards')
root.geometry("500x500")


def balancing():
balancing_frame.pack(fill="both", expand=1)



global show_balancing
show_balancing = Label(balancing_frame)
show_balancing.pack(pady=15)

global balancing
balancing = ['balanced1', 'balanced2', 'balanced3', 'balanced4', 'balanced5', 'unbalanced1', 'unbalanced2', 'unbalanced3', 'unbalanced4', 'unbalanced5']

global balancing_state
balancing_state = {
'balanced1':'balanced',
'balanced2':'balanced',
'balanced3':'balanced',
'balanced4':'balanced',
'balanced5':'balanced',
'unbalanced1':'unbalanced',
'unbalanced2':'unbalanced',
'unbalanced3':'unbalanced',
'unbalanced4':'unbalanced',
'unbalanced5':'unbalanced',

}

answer_list = []
count = 1


while count < 3:
    rando = randint(0, len(balancing_state)-1)
    if count == 1:
        answer = balancing[rando]

        global balancing_image
        balancing = "C:/Users/Kisitu/Desktop/project/balancing/" + balancing[rando] + ".png"
        balancing_image = ImageTk.PhotoImage(Image.open(balancing))
        show_balancing.config(image=balancing_image)

    answer_list.append(balancing[rando])

    '''random.shuffle(balancing)'''
    count += 1

    random.shuffle(answer_list)

global balancing_radio
balancing_radio = IntVar()

balancing_radio_butto1 = Radiobutton(balancing_frame, text = balancing[answer_list[0]], variable=balancing_radio, value = 1)
balancing_radio_butto1.pack(pady=10)
balancing_radio_butto2 =  Radiobutton(balancing_frame, text = balancing[answer_list[1]], variable=balancing_radio, value = 2).pack()




my_menu = Menu(root)
root.config(menu=my_menu, bg='#B7F7BB')

lesson_menu = Menu(my_menu)
my_menu.add_cascade(label="Lesson", menu=lesson_menu)
lesson_menu.add_command(label="balancing", command=balancing)
lesson_menu.add_separator()
lesson_menu.add_command(label="Exit", command=root.quit)


balancing_frame = Frame(root, width=500, height=500, )



root.mainloop()

... text = balancing[answer_list[0]]...

balancing is a list, you are trying to index a value from the list.平衡是一个列表,您正在尝试从列表中索引一个值。 you are passing answer_list[0] as index.您将 answer_list[0] 作为索引传递。 answer_list contains random strings from balancing. answer_list 包含来自平衡的随机字符串。 you are trying to index a list with a string like in您正在尝试使用类似 in 的字符串索引列表

balancing["balanced2"]

maybe you could use a dictionary?也许你可以使用字典?

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

相关问题 Python错误:“ TypeError:字符串索引必须为整数” - Python Error:“TypeError: string indices must be integers” Python错误TypeError:字符串索引必须是整数 - Python error TypeError: string indices must be integers 如何修复 Python 中的“TypeError:字符串索引必须是整数”错误 - How to fix "TypeError: string indices must be integers" error in Python Python 和 JSON 错误 - TypeError:字符串索引必须是整数 - Python and JSON Error - TypeError: string indices must be integers python和json,错误-TypeError:字符串索引必须为整数 - python and json, Error - TypeError: string indices must be integers 类型错误:使用 Python 解析 Json 时,字符串索引必须是整数错误 - TypeError: string indices must be integers error when parsing Json with Python Python和网络X中的错误-TypeError:字符串索引必须是整数,而不是str - Error in Python and network X - TypeError: string indices must be integers, not str 如何在Python中修复&#39;TypeError:字符串索引必须是整数&#39;错误 - How to fix 'TypeError: string indices must be integers' error in Python Python Json TypeError:字符串索引必须是整数错误 - Python Json TypeError: string indices must be integers error Python:类型错误:字符串索引必须是 python 中的整数 - Python: TypeError: string indices must be integers in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM