Tkinter Chatbot语音气泡设计

时间:2019-05-06 12:52:41

标签: python-3.x tkinter chatbot

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

val df = spark.sparkContext.binaryFiles(filePath, 12)
  .mapValues(content => new String(content.toArray(), StandardCharsets.ISO_8859_1))
  .toDF

1 个答案:

答案 0 :(得分:0)

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

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()