Chatbot语音气泡设计

Chatbot语音气泡设计

本文介绍了Tkinter Chatbot语音气泡设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我对我的英语不好感到抱歉。我正在与tkinter设计一个业余聊天机器人。我想在对话框窗口中添加类似whatsapp的气球。我也希望这些著作从下而上地出现在窗口中。我尝试将其用作画布,但没有发生。我尝试做为标签,但出现了错误。如果您可以帮助我,我们将不胜感激。 TypeError:只能将str(而不是 Canvas)连接到str。

First I'm sorry for my bad english. I am designing a chatbot with the tkinter as an amateur. I want to add whatsapp-like balloons to the dialog window. I also want the writings to go from the bottom up in the window. I tried to do it as a canvas but it didn't happen. I tried as a label but gave an error.I'd appreciate it if you could help me with that. "TypeError: can only concatenate str (not "Canvas") to str".The sample design I want

import tkinter as tk
from tkinter import *
import random
import re
import datetime
from tkinter import messagebox
from tkinter.font import Font
root = tk.Tk()
root.title('deneme')
root.geometry('410x600+400+100')
root.config(bg='lightblue')
buton_işlevi=StringVar(root)
buton_işlevi.set("")


#buton oluşturma ve konumu
buton = tk.Button(root, width=10, height=2, relief='raised',state='active',command=lambda :add_text(pencere,giriş,buton_işlevi))
buton.pack()
buton.place(x=310, y=550)
buton.config(text='GÖNDER', bg='lightblue', font='Verdana 8 bold')
root.bind('<Return>',lambda x:add_text(pencere,giriş,buton_işlevi))

#mesaj yazma alanı ve konumu
giriş = tk.Entry(root, textvariable=buton_işlevi,font=('NanumGothic', 12))
giriş.pack()
giriş.place(x=10, y=550, width=290, height=40)

canvas = Canvas(width=300, height=300, bg='white')
canvas.create_oval(200, 200, 300, 300, width=5, fill='red')

#pencere ve konumu
pencere = tk.Text(root, bg='white', yscrollcommand='YES', font=('NanumGothic', 12))
pencere.config(state='disabled')
pencere.pack()
pencere.place(x=10, y=10, width=390, height=530)


#bot ve user mesaj arka plan rengi
pencere.tag_config('bot_renk', background='#fccbc7', foreground='black')
pencere.tag_config('user_renk', background='yellow')
pencere.config(state='normal')

#bot karşılama mesajı
karşılama = 'Hoşgeldiniz'
soru1  = ['merhaba', 'selam']
cevap1 = ["sanada", "iyiyim"]
soru2  = ["deneme", "merhaba", "selam"]

pencere.insert('end', '\nBOT:\t')
pencere.insert('end', karşılama +'\n', 'bot_renk')


#Mesaj ayarları
def add_text(mw,st,imsg):
#mw:message window/st:state/imsg:input message
    bot_mesaj=""
    user_mesaj = imsg.get()

    if user_mesaj in soru1:
        user_mesaj='\nUSER :\t'+imsg.get()+'\n'+canvas
        bot_mesaj = '\nBOT  :\t'+ random.choice(cevap1)+'\n'
        mw.config(state='normal')
        mw.insert('end',user_mesaj, 'user_renk')
        mw.insert('end',bot_mesaj, 'bot_renk')
        imsg.set("")
        mw.see('end')
        mw.config(state='disabled')

    elif user_mesaj in soru2:
        user_mesaj='\nERÇİN   :\t'+imsg.get()+'\n'
        bot_mesaj='\nBOT   :\t'+ random.choice(cevap2)+'\n'
        mw.config(state='normal')
        mw.insert('end',user_mesaj, 'user_renk')
        mw.insert('end',bot_mesaj, 'bot_renk')
        imsg.set("")
        mw.see('end')
        mw.config(state='disabled')

    else :
        user_mesaj='\nUSER:\t' +imsg.get()+'\n'
        bot_mesaj='\nBOT:\t' + 'Bu kelimeyi henüz öğrenmedim' +'\n'
        mw.config(state='normal')
        mw.insert('end',user_mesaj, 'user_renk')
        mw.insert('end',bot_mesaj, 'bot_renk')
        imsg.set("")
        mw.see('end')
        mw.config(state='disabled')

root.mainloop()


推荐答案

我运行了您的代码,没有错误。但是,既然您提到了Whatsapp气泡,这是您可以使用的基本版本。

I ran your code and there is no error. But since you mentioned about Whatsapp bubble, here is a basic version you can work on.

from tkinter import *
from datetime import datetime

root = Tk()
root.config(bg="lightblue")

canvas = Canvas(root, width=200, height=200,bg="white")
canvas.grid(row=0,column=0,columnspan=2)

bubbles = []

class BotBubble:
    def __init__(self,master,message=""):
        self.master = master
        self.frame = Frame(master,bg="light grey")
        self.i = self.master.create_window(90,160,window=self.frame)
        Label(self.frame,text=datetime.now().strftime("%Y-%m-%d %H:%m"),font=("Helvetica", 7),bg="light grey").grid(row=0,column=0,sticky="w",padx=5)
        Label(self.frame, text=message,font=("Helvetica", 9),bg="light grey").grid(row=1, column=0,sticky="w",padx=5,pady=3)
        root.update_idletasks()
        self.master.create_polygon(self.draw_triangle(self.i), fill="light grey", outline="light grey")

    def draw_triangle(self,widget):
        x1, y1, x2, y2 = self.master.bbox(widget)
        return x1, y2 - 10, x1 - 15, y2 + 10, x1, y2

def send_message():
    if bubbles:
        canvas.move(ALL, 0, -65)
    a = BotBubble(canvas,message=entry.get())
    bubbles.append(a)

entry = Entry(root,width=26)
entry.grid(row=1,column=0)
Button(root,text="Send",command=send_message).grid(row=1,column=1)
root.mainloop()

这篇关于Tkinter Chatbot语音气泡设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:32