Python 3.x Python:Tkinter聊天应用程序-我想在消息周围画气泡

Python 3.x Python:Tkinter聊天应用程序-我想在消息周围画气泡,python-3.x,tkinter,chat,Python 3.x,Tkinter,Chat,我正在学习如何使用Tkinter制作GUI。我创建了一个简单的聊天应用程序 我希望聊天文本(仅聊天文本,而不是日期和时间)像大多数聊天应用程序(WhatsApp、Messenger等)一样包装在气泡(或者至少是矩形)中 我已经试着解决这个问题好几天了,但我似乎不知道怎么做 可以对我的代码进行这样的编辑吗?(非常感谢您的评论,以便我可以学习) 从日期时间导入日期时间 从tkinter进口* 导入文本包装 def发送(事件): getmsg=EntryBox.get(“1.0”,“end-1c”).

我正在学习如何使用Tkinter制作GUI。我创建了一个简单的聊天应用程序

我希望聊天文本(仅聊天文本,而不是日期和时间)像大多数聊天应用程序(WhatsApp、Messenger等)一样包装在气泡(或者至少是矩形)中

我已经试着解决这个问题好几天了,但我似乎不知道怎么做

可以对我的代码进行这样的编辑吗?(非常感谢您的评论,以便我可以学习)

从日期时间导入日期时间
从tkinter进口*
导入文本包装
def发送(事件):
getmsg=EntryBox.get(“1.0”,“end-1c”).strip()
msg=textwrap.fill(getmsg,30)
EntryBox.delete(“0.0”,结束)
如果消息!='':
ChatLog.config(状态=正常)
插入(结束,当前时间(“小”、“右”、“颜色”))
插入(结束,msg+'\n\n',(“右”))
ChatLog.config(前台=“#0000CC”,字体=(“Helvetica”,9))
res=“机器人的响应进入此处,将此消息拉长以测试textwrap”
插入(结束,当前时间,(“小”,“颜色”))
ChatLog.insert(END,textwrap.fill(res,30)+'\n\n')
ChatLog.config(状态=已禁用)
ChatLog.yview(完)
def发送方式按钮():
getmsg=EntryBox.get(“1.0”,“end-1c”).strip()
msg=textwrap.fill(getmsg,30)
EntryBox.delete(“0.0”,结束)
如果消息!='':
ChatLog.config(状态=正常)
插入(结束,当前时间(“小”、“右”、“颜色”))
插入(结束,msg+'\n\n',(“右”))
ChatLog.config(前台=“#0000CC”,字体=(“Helvetica”,9))
res=“机器人的响应进入此处,将此消息拉长以测试textwrap”
插入(结束,当前时间,(“小”,“颜色”))
ChatLog.insert(END,textwrap.fill(res,30)+'\n\n')
ChatLog.config(状态=已禁用)
ChatLog.yview(完)
#定义以下两个函数来添加或删除占位符文本。
def删除占位符(事件):
占位符
EntryBox.focus\u set()
def添加占位符(事件):
如果占位符标志==1:
占位符位置(x=6,y=421,高度=70,宽度=265)
base=Tk()
基本标题(“示例聊天”)
基础几何(“400x500”)
可调整大小(宽度=假,高度=假)
#向GUI添加菜单
主菜单=菜单(基本)
文件菜单=菜单(基本)
文件菜单。添加命令(label=“New…”)
文件菜单。添加命令(label=“另存为…”)
文件菜单。添加命令(label=“Exit”)
主菜单。添加级联(label=“File”,menu=文件菜单)
#将其余菜单选项添加到主菜单
主菜单。添加命令(label=“Edit”)
主菜单。添加命令(label=“Quit”)
base.config(菜单=主菜单)
now=datetime.now()
当前时间=现在.strftime(“%D-%H:%M\n”)
#创建聊天窗口
ChatLog=Text(基本,bd=0,height=“8”,width=“50”,font=“Helvetica”,wrap=“word”)
ChatLog.config(状态=正常)
ChatLog.tag_config(“right”,justify=“right”)
tag_config(“小”,字体=(“Helvetica”,7))
ChatLog.tag_config(“color”,前台=“#333”)
插入(结束,当前时间,(“小”,“颜色”))
insert(END,textwrap.fill(f“Hello{'*Name*},我能为您提供什么帮助?”,30))
ChatLog.insert(结束,“\n”)
ChatLog.config(前台=“#0000CC”,字体=(“Helvetica”,9))
ChatLog.config(状态=已禁用)
#将滚动条绑定到聊天窗口
scrollbar=scrollbar(基本,命令=ChatLog.yview,cursor=“双箭头”)
ChatLog['yscrollcommand']=scrollbar.set
#创建按钮发送消息
SendButton=按钮(底部,字体=(“Comic Sans MS”,12,“粗体”),text=“Send”,width=“8”,height=5,
bd=0,fg=“#750216”,activebackground=“#AAAAAA”,bg=“#99999”,command=send_by_按钮)
#创建用于输入消息的框
EntryBox=Text(base,bd=0,fg=“#000000”,bg=“#fff5f5”,highlightcolor=“#750216”,
width=“29”,height=“5”,font=(“Arial”,10),wrap=“word”)
#占位符配置和文本:
占位符=文本(基本,bd=0,fg=“#A0A0”,bg=“#fff5f5”,highlightcolor=“#750216”,
width=“29”,height=“5”,font=(“Arial”,10),wrap=“word”)
占位符。插入(“1.0”,“提出问题(即我的好处是什么?)
#将所有组件放置在屏幕上
滚动条位置(x=376,y=6,高度=406)
聊天记录位置(x=6,y=6,高度=410,宽度=370)
入口盒位置(x=6,y=421,高度=70,宽度=276)
SendButton.place(x=282,y=421,高度=70)
占位符位置(x=6,y=421,高度=70,宽度=276)
占位符.bind(“,deletep占位符)
EntryBox.bind(“,addPlaceholder)
#每0.1秒刷新一次GUI窗口,主要用于“发送”按钮。
#如果输入框不包含文本-->“发送”按钮处于非活动状态,否则它将被激活。
def update():
全局占位符标志
if(EntryBox.get(“1.0”,“end-1c”).strip()='':
SendButton['state']=已禁用
占位符标志=1
elif EntryBox.get(“1.0”,“end-1c”).strip()!=“”:
SendButton['state']=活动
占位符标志=0
基本。之后(100,更新)
base.bind(“”,send)
更新()
base.mainloop()

acw1668在上面的评论中给了我一个提示,感谢他,我可以找到解决方案。您可以在
Text
小部件中使用
window\u create()
方法

我将功能
send
更改为此,现在可以工作了:

def send(event):
    msg = EntryBox.get("1.0", 'end-1c').strip()
    EntryBox.delete("0.0", END)

    if msg != '':
        ChatLog.config(state=NORMAL)
        ChatLog.insert(END, current_time+' ', ("small", "right", "greycolour"))
        ChatLog.window_create(END, window=Label(ChatLog, fg="#000000", text=msg, 
        wraplength=200, font=("Arial", 10), bg="lightblue", bd=4, justify="left"))
        ChatLog.insert(END,'\n ', "left")
        ChatLog.config(foreground="#0000CC", font=("Helvetica", 9))
        ChatLog.yview(END)

        res = "Bot's response goes into here, elongating this message to test textwrap"
        ChatLog.insert(END, current_time+' ', ("small", "greycolour", "left"))
        ChatLog.window_create(END, window=Label(ChatLog, fg="#000000", text=res, 
        wraplength=200, font=("Arial", 10), bg="#DDDDDD", bd=4, justify="left"))
        ChatLog.insert(END, '\n ', "right")
        ChatLog.config(state=DISABLED)
        ChatLog.yview(END)
还请注意,我添加了一个新的标记配置以证明左对齐:


ChatLog.tag\u config(“left”,justify=“left”)

我不确定您是否可以使用
文本
小部件执行您想要的操作,但您应该可以使用
画布
来执行。为了保持简单,您是否考虑更改消息的背景以创建围绕它的矩形的幻觉?@ ATLAS435,请您帮助我如何使用画布代替文本重做代码?如果你能给我一个小例子,我可以继续。这将是很多代码,我有自己的项目要做。试着问问自己,如果你不能解决一个问题。另外一个可能的解决方案是使用
place(x=…,y=…)
draw a
def send(event):
    msg = EntryBox.get("1.0", 'end-1c').strip()
    EntryBox.delete("0.0", END)

    if msg != '':
        ChatLog.config(state=NORMAL)
        ChatLog.insert(END, current_time+' ', ("small", "right", "greycolour"))
        ChatLog.window_create(END, window=Label(ChatLog, fg="#000000", text=msg, 
        wraplength=200, font=("Arial", 10), bg="lightblue", bd=4, justify="left"))
        ChatLog.insert(END,'\n ', "left")
        ChatLog.config(foreground="#0000CC", font=("Helvetica", 9))
        ChatLog.yview(END)

        res = "Bot's response goes into here, elongating this message to test textwrap"
        ChatLog.insert(END, current_time+' ', ("small", "greycolour", "left"))
        ChatLog.window_create(END, window=Label(ChatLog, fg="#000000", text=res, 
        wraplength=200, font=("Arial", 10), bg="#DDDDDD", bd=4, justify="left"))
        ChatLog.insert(END, '\n ', "right")
        ChatLog.config(state=DISABLED)
        ChatLog.yview(END)