gpt4 book ai didi

python - 按 tkinter 按钮自动更新标签

转载 作者:行者123 更新时间:2023-12-01 08:41:34 24 4
gpt4 key购买 nike

我正在尝试使用 CHATTERBOT MODULE 和 TKINTER 创建一个聊天机器人程序。几乎没问题,事实上我的问题是,每次单击按钮,程序都会使用命令 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()

最佳答案

要解决此问题,您可以将标签(仅一次)打包在按钮之后,因此代码的最后一部分如下所示:

bottone = Button(schermata,text='PARLA CON OMERO',command=command)
bottone.pack()

risposta = Label(schermata, text="")
risposta.pack()

schermata.mainloop()

然后,更改命令函数,使其仅更新已打包标签的文本:

def command():
global risposta
user_input = input.get()
response = bot.get_response(user_input)
risposta['text']=str(response.text)

PS:由于您尚未提供 .txt 文件,因此我无法执行 with 范围。对于您的下一篇文章,请考虑提供 MCVE .

关于python - 按 tkinter 按钮自动更新标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53469489/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com