【问题标题】:Exception in Tkinter callback: IndexError: list index out of rangeTkinter 回调中的异常:IndexError:列表索引超出范围
【发布时间】:2020-12-27 15:41:51
【问题描述】:

我正在使用 Tkinter 模块创建一个 GUI 文件来构建聊天机器人的桌面应用程序结构。但是,我不断收到此错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/Users/bellacho/opt/anaconda3/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
    return self.func(*args)
  File "<ipython-input-3-466bf200dfb6>", line 70, in send
    res = getResponse(ints, intents)
  File "<ipython-input-3-466bf200dfb6>", line 49, in getResponse
    tag = ints[0]['intent']
IndexError: list index out of range

知道如何解决这个问题吗?请让我知道,提前谢谢你。 这是我的代码:


def getResponse(ints, intents_json):
    tag = ints[0]['intent']
    list_of_intents = intents_json['intents']
    for i in list_of_intents:
        if(i['tag']== tag):
            result = random.choice(i['responses'])
            break
    return result

#Creating tkinter GUI
import tkinter
from tkinter import *

def send():
    msg = EntryBox.get("1.0",'end-1c').strip()
    EntryBox.delete("0.0",END)

    if msg != '':
        ChatBox.config(state=NORMAL)
        ChatBox.insert(END, "You: " + msg + '\n\n')
        ChatBox.config(foreground="#446665", font=("Verdana", 12 )) 
        ints = predict_class(msg)
        res = getResponse(ints, intents)
        

        ChatBox.insert(END, "Bot: " + res + '\n\n')           

        ChatBox.config(state=DISABLED)

        ChatBox.yview(END)

root = Tk()
root.title("Chatbot")
root.geometry("400x500")
root.resizable(width=FALSE, height=FALSE)

【问题讨论】:

  • ints 好像是空的
  • predict_class(msg) 是做什么的?什么是ints

标签: python user-interface tkinter


【解决方案1】:

ints 不是一个列表,所以 getResponse 函数有问题,因为你是这样对待它的 尝试重新制作getResponse 函数 然后看看你需要如何处理整数

【讨论】:

  • 如果ints 不是列表,则不会出现IndexError: list index out of range 错误。它会得到类似TypeError: 'xxx' object is not subscriptable 的东西。
猜你喜欢
  • 1970-01-01
  • 2022-11-15
  • 1970-01-01
  • 2011-10-31
  • 2015-06-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多