Python-tkinter-聊天窗口GUI

  • 环境python3.x 3.x之后tkinter自带,jupyter notebook/pycharm

1|0常见的聊天窗口

image.png
  • 聊天窗口布局
左上:聊天历史信息显示 左中:当前信息编辑区域 左下:按钮区域 右侧:显示展示区域

 

  • Frame控件

容器区域布局

frmLT,frmLC,frmLB,frmRT

#创建frmLT容器 frmLT = Frame(width = 500, height = 320, bg = 'white') frmLC = Frame(width = 500, height = 150, bg = 'white') frmLB = Frame(width = 500, height = 30) frmRT = Frame(width = 200, height = 500)

 

 

  • 控件对象命名规则

“控件类型” + “功能”

frmLT: frame + LeftTop txtMsg: text控件 + 消息 btnSend: button控件 + 发送

 

 
  • 消息处理:
txtMsg = Text(frmLC) txtMsg.bind("<KeyPress-Up>", sendMsgEvent) btnSend = Button(frmLB, text = '发送', width = 8, command = sendMsg) btnCancel =Button(frmLB, text = '取消', width = 8, command = cancelMsg)

 

  • 图片处理:
imgInfo = PhotoImage(file = "timg-2.gif") lblImage = Label(frmRT, image = imgInfo) lblImage.image = imgInfo

 

  • sendNsg()

回调函数,通过函数指针调用的函数

def sendMsg():#发送消息 strMsg = "我:" + time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())+ '\n' txtMsgList.insert(END, strMsg, 'greencolor') txtMsgList.insert(END, txtMsg.get('0.0', END)) txtMsg.delete('0.0', END)

 


  • cancelMsg()
def cancelMsg():#取消信息 txtMsg.delete('0.0', END)

 


  • sendMsgEvent(event)
def sendMsgEvent(event):#发送消息事件 if event.keysym =='Up': sendMsg()

 


  • grid()界面布局控制
frmLT.grid(row = 0, column = 0, columnspan = 2, padx = 1, pady = 3) frmLC.grid(row = 1, column = 0, columnspan = 2, padx = 1, pady = 3) frmLB.grid(row = 2, column = 0, columnspan = 2) frmRT.grid(row = 0, column = 2, rowspan = 3, padx =2, pady = 3) #固定大小 frmLT.grid_propagate(0) frmLC.grid_propagate(0) frmLB.grid_propagate(0) frmRT.grid_propagate(0) btnSend.grid(row = 2, column = 0) btnCancel.grid(row = 2, column = 1) lblImage.grid() txtMsgList.grid() txtMsg.grid()

 

  • 主事件循环
app.mainloop()

 

  • 代码整体:
from tkinter import * import time def main(): def sendMsg():#发送消息 strMsg = "我:" + time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())+ '\n' txtMsgList.insert(END, strMsg, 'greencolor') txtMsgList.insert(END, txtMsg.get('0.0', END)) txtMsg.delete('0.0', END) def cancelMsg():#取消信息 txtMsg.delete('0.0', END) def sendMsgEvent(event):#发送消息事件 if event.keysym =='Up': sendMsg() #创建窗口 app = Tk() app.title('与python聊天') #创建frame容器 frmLT = Frame(width = 500, height = 320, bg = 'white') frmLC = Frame(width = 500, height = 150, bg = 'white') frmLB = Frame(width = 500, height = 30) frmRT = Frame(width = 200, height = 500) #创建控件 txtMsgList = Text(frmLT) txtMsgList.tag_config('greencolor',foreground = '#008C00')#创建tag txtMsg = Text(frmLC) txtMsg.bind("<KeyPress-Up>", sendMsgEvent) btnSend = Button(frmLB, text = '发送', width = 8, command = sendMsg) btnCancel =Button(frmLB, text = '取消', width = 8, command = cancelMsg) imgInfo = PhotoImage(file = "timg-2.gif") lblImage = Label(frmRT, image = imgInfo) lblImage.image = imgInfo #窗口布局 frmLT.grid(row = 0, column = 0, columnspan = 2, padx = 1, pady = 3) frmLC.grid(row = 1, column = 0, columnspan = 2, padx = 1, pady = 3) frmLB.grid(row = 2, column = 0, columnspan = 2) frmRT.grid(row = 0, column = 2, rowspan = 3, padx =2, pady = 3) #固定大小 frmLT.grid_propagate(0) frmLC.grid_propagate(0) frmLB.grid_propagate(0) frmRT.grid_propagate(0) btnSend.grid(row = 2, column = 0) btnCancel.grid(row = 2, column = 1) lblImage.grid() txtMsgList.grid() txtMsg.grid() #主事件循环 app.mainloop() if __name__ == "__main__": main()

 

 
链接:https://www.jianshu.com/p/93481b3b4f1e
https://blog.csdn.net/kun_dl/article/details/79500962

 


__EOF__

本文作者komomon
本文链接https://www.cnblogs.com/forforever/p/12894343.html
关于博主:喜欢读书、旅行、爬山。评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   komomon  阅读(2732)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示