Tkinter Chatbot Speech Bubbles design

后端 未结 1 786
伪装坚强ぢ
伪装坚强ぢ 2021-01-28 06:45

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

相关标签:
1条回答
  • 2021-01-28 07:36

    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()
    
    0 讨论(0)
提交回复
热议问题