我正在尝试用CHATTERBOT模块和TKINTER创建一个chatbot程序。
几乎没问题,事实上我的问题是,每次点击按钮,程序都会用命令risposta.pack()
给我创建新的标签。
我的意图是只创建一个标签,并每点击一次按钮就更新一次。
我该怎么做?你知道吗
我的编码:
from chatterbot import ChatBot
from tkinter import *
import time
from chatterbot.trainers import ListTrainer
bot = ChatBot(
"GUI Bot",
storage_adapter="chatterbot.storage.SQLStorageAdapter",
input_adapter='chatterbot.input.VariableInputTypeAdapter',
output_adapter='chatterbot.output.OutputAdapter',
database='../database,db',
logic_adapters=[
{
"import_path": "chatterbot.logic.BestMatch",
"statement_comparison_function": "chatterbot.comparisons.levenshtein_distance",
"response_selection_method": "chatterbot.response_selection.get_first_response"
}
]
)
with open('/home/griguols/Scrivania/chatterbot/istruzioni.txt') as istruzioni:
conversation = istruzioni.readlines()
bot.set_trainer(ListTrainer)
bot.train(conversation)
def command():
global risposta
user_input = input.get()
response = bot.get_response(user_input)
risposta = Label(schermata, text=str(response.text))
risposta.pack()
schermata = Tk()
ment = StringVar()
schermata.geometry('1000x500')
schermata.title('OMERO')
titolo = Label(schermata,text='OMERO')
titolo.pack()
input = Entry(schermata,textvariable=ment)
input.pack()
bottone = Button(schermata,text='PARLA CON OMERO',command=command)
bottone.pack()
schermata.mainloop()
要解决此问题,可以将
label
(仅一次)打包在button
之后,因此代码的最后一部分如下所示:然后,更改命令函数,使其仅更新已打包标签的文本:
PS:我无法执行
with
作用域,因为您没有提供.txt文件。对于下一篇文章,请考虑提供一个MCVE。你知道吗相关问题 更多 >
编程相关推荐