【问题标题】:Auto-update label with the pressing of a tkinter button按下 tkinter 按钮自动更新标签
【发布时间】:2018-11-25 16:24:45
【问题描述】:

我正在尝试使用 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()

【问题讨论】:

    标签: python python-3.x tkinter chatterbot


    【解决方案1】:

    为了解决这个问题,你可以在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 文件。对于您的下一篇文章,请考虑提供MCVE。

    【讨论】:

    • 非常感谢 David Duran,但 MCVE 到底是什么?
    • 不客气。请考虑接受答案以关闭问题。如链接中所述,MCVE 是一个最小、完整、可验证的示例。
    猜你喜欢
    • 1970-01-01
    • 2019-02-19
    • 1970-01-01
    • 2020-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多