按下 tkinter 按钮自动更新标签
Auto-update label with the pressing of a tkinter button
我正在尝试使用 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()
为了解决这个问题,可以在button
之后打包label
(仅一次),所以最后一部分代码如下:
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:我无法执行 with
作用域,因为您没有提供 .txt 文件。下一个 post,请考虑提供一个 MCVE。
我正在尝试使用 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()
为了解决这个问题,可以在button
之后打包label
(仅一次),所以最后一部分代码如下:
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:我无法执行 with
作用域,因为您没有提供 .txt 文件。下一个 post,请考虑提供一个 MCVE。