聊天机器人的单选按钮

时间:2020-05-13 11:09:42

标签: python python-3.x tensorflow tkinter chatbot

我试图使用带有Tensorflow库的python创建一个聊天机器人。我需要为特定问题添加单选按钮功能。我可以在chatbot窗口中获得单选按钮选项,如下所示 enter image description here

我需要单选按钮选项作为聊天记录(作为聊天记录)。但是,我只能将它作为字符串添加到聊天日志中。请查看代码并提供宝贵的反馈意见。

def bot_speak(str):
     ChatLog.config(state=NORMAL)
     ChatLog.insert(END, "Bot: " + str + '\n\n')
     ChatLog.yview(END)

def user_speak(str):
    ChatLog.config(state=NORMAL)
    ChatLog.insert(END, "You: " + str + '\n\n')
    ChatLog.yview(END)


def radio():
    def sel():
       selection = "You selected the option " + str(var.get())
       label.config(text = selection)

    var = IntVar()
    R1 = Radiobutton(
        base, text="Option 1", variable=var, value=1, command=sel
    )

    R1.pack( anchor = W )
    R2 = Radiobutton(
        base, text="Option 2", variable=var, value=2, command=sel
    ) 
    R2.pack( anchor = W )

    R3 = Radiobutton(
        base, text="Option 3", variable=var, value=3, command=sel
    )
    R3.pack( anchor = W)

    label = Label(base)
    label.pack()


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

        elif booll_1 == True:
            var1=identify_num(msg)
            if(till!=None):
                user_speak(msg)
                #some function
                bool_1=False
        else:
            user_speak(msg)
            res = chatbot_response(msg)
            bot_speak(res)

def call(event):
    send()

#Creating GUI with tkinter
base = Tk()
base.title(" Chatbot")
base.geometry("400x500")
base.resizable(width=FALSE, height=FALSE)
#Create Chat window
ChatLog = Text(base, bd=0, bg="white", height="8", width="50", font="Arial",)
ChatLog.config(state=DISABLED)
ChatLog.config(foreground="#442265", font=("Verdana", 12 ))
#Bind scrollbar to Chat window
scrollbar = Scrollbar(base, command=ChatLog.yview, cursor="heart")
ChatLog['yscrollcommand'] = scrollbar.set
#Create Button to send message
base.bind('<Return>',call )

SendButton = Button(
    base, font=("Verdana",12,'bold'), 
    text="Send", width="12", height=5,
    bd=0, bg="#32de97", activebackground="#3c9d9b",
    fg='#ffffff', command= radio 
)
#Create the box to enter message
EntryBox = Text(base, bd=0, bg="white",width="29", height="5", font="Arial")

#Place all components on the screen
scrollbar.place(x=376,y=6, height=386)
ChatLog.place(x=6,y=6, height=386, width=370)
EntryBox.place(x=128, y=401, height=90, width=265)
SendButton.place(x=6, y=401, height=90)
base.mainloop()

0 个答案:

没有答案