怎么用python实现简易聊天对话框
发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,本篇内容介绍了"怎么用python实现简易聊天对话框"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!效
千家信息网最后更新 2026年10月31日怎么用python实现简易聊天对话框
本篇内容介绍了"怎么用python实现简易聊天对话框"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
效果图:
客户端代码:
import tkinter as tkfrom tkinter import scrolledtextimport socketimport threadingfrom datetime import datetime def tcp_recv(sock): while True: str = sock.recv(1024).decode("utf-8") show_info(str)def send_func(sock): str = send_msg.get("0.0", "end") sock.send(str.encode("utf-8")) show_info(str) def show_info(str): now = datetime.now() s_time = now.strftime("%Y-%m-%d %H:%M:%S") str = str.rstrip() if len(str) == 0: return -1 send_msg.delete("0.0", "end") temp = s_time + "\n " + str + "\n" show_msg.insert(tk.INSERT, "%s" % temp) msFont = '微软雅黑' #字体fontSize = 18 #字体大小sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)sock.connect(("127.0.0.1",8888)) mainWindow = tk.Tk()mainWindow.title("客户端")mainWindow.minsize(400,400)show_msg = scrolledtext.ScrolledText(mainWindow,font=(msFont,fontSize))show_msg.place(width=400,height=250,x=0,y=0)#show_msg.insert(tk.INSERT,"%s 已连接\n"%addr[0])send_msg = scrolledtext.ScrolledText(mainWindow,font=(msFont,fontSize))send_msg.place(width=400,height=140,x=0,y=260)button_send = tk.Button( mainWindow, font=(msFont,fontSize),text = "发 送",bg="orange",fg="white", command=lambda:send_func(sock))button_send.place(width=100,height=40,x=300,y=360) t = threading.Thread(target=tcp_recv,args=(sock,))t.start()tk.mainloop()服务器代码:
import tkinter as tkfrom tkinter import scrolledtextimport socketimport threadingfrom datetime import datetime def tcp_recv(sock): while True: str = sock.recv(1024).decode("utf-8") show_info(str)def send_func(sock): str = send_msg.get("0.0", "end") sock.send(str.encode("utf-8")) show_info(str) def show_info(str): now = datetime.now() s_time = now.strftime("%Y-%m-%d %H:%M:%S") str = str.rstrip() if len(str) == 0: return -1 send_msg.delete("0.0", "end") temp = s_time + "\n " + str + "\n" show_msg.insert(tk.INSERT, "%s" % temp) msFont = '微软雅黑' #字体fontSize = 18 #字体大小sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)sock.bind(("127.0.0.1",8888))sock.listen(5) mainWindow = tk.Tk()mainWindow.title("服务器")mainWindow.minsize(400,400)show_msg = scrolledtext.ScrolledText(mainWindow,font=(msFont,fontSize))show_msg.place(width=400,height=250,x=0,y=0)send_msg = scrolledtext.ScrolledText(mainWindow,font=(msFont,fontSize))send_msg.place(width=400,height=140,x=0,y=260)button_send = tk.Button( mainWindow, font=(msFont,fontSize),text = "发 送",bg="orange",fg="white", command=lambda:send_func(s))button_send.place(width=100,height=40,x=300,y=360) s,addr = sock.accept()t = threading.Thread(target=tcp_recv,args=(s,))t.start()tk.mainloop()"怎么用python实现简易聊天对话框"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
字体
utf-8
对话框
简易
对话
代码
内容
大小
客户
客户端
更多
服务器
知识
微软
服务
实用
学有所成
接下来
困境
实际
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
小米移动互联网科技园
互联网加科技
科技与互联网
互联网最新的科技有限公司
上海挚品互联网科技
互联网科技创新企业排名
玖二柒互联网科技公司待遇
天速互联网络科技有限公司
互联网科技实习生岗位职责
互联网对科技的发展
和信科技工业互联网
国大互联网教育科技有限公司工作
畅思互联网科技
互联网科技大赛获奖感言
科技赋能视角下互联网保险发展
锡山区互联网智能科技产品供应
萌小萌互联网科技河南有限公司
普陀区互联网教育科技管理办法
互联网公司属于科技吗
上海万顺科技互联网有限公司招聘
互联网最新的科技有限公司
重庆互联网络科技
互联网公司 科技公司
猪兼强互联网科技
易通互联网络科技有限公司
中国互联网金融科技基金
科技伦理 互联网公司
苏州五指互联网络科技有限
注册互联网科技公司经营范围大全
企晟互联网信息科技有限公司