python实现简易聊天对话框

 更新时间:2022年02月07日 09:53:20   作者:丶蓝色  
这篇文章主要为大家详细介绍了python实现简易聊天对话框,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了python实现简易聊天对话框的具体代码,供大家参考,具体内容如下

效果图: 

客户端代码: 

import tkinter as tk
from tkinter import scrolledtext
import socket
import threading
from 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 tk
from tkinter import scrolledtext
import socket
import threading
from 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实现http接口自动化测试的示例代码

    Python实现http接口自动化测试的示例代码

    这篇文章主要介绍了Python实现http接口自动化测试的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • Python日志模块logging简介

    Python日志模块logging简介

    这篇文章主要介绍了Python日志模块logging简介,本文讲解了Logger、Handler、Formatter、日志配置管理、通过文件配置管理日志等内容,需要的朋友可以参考下
    2015-04-04
  • 简单了解python的一些位运算技巧

    简单了解python的一些位运算技巧

    这篇文章主要介绍了简单了解python的一些位运算技巧,位运算的性能大家想必是清楚的,效率绝对高。相信爱好源码的同学,在学习阅读源码的过程中会发现不少源码使用了位运算,需要的朋友可以参考下
    2019-07-07
  • Python流程控制 while循环实现解析

    Python流程控制 while循环实现解析

    这篇文章主要介绍了Python流程控制 while循环实现解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09
  • python输出pdf文档的实例

    python输出pdf文档的实例

    今天小编就为大家分享一篇python输出pdf文档的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-02-02
  • django使用channels实现通信的示例

    django使用channels实现通信的示例

    这篇文章主要介绍了django使用channels实现通信的示例,帮助大家更好的理解和学习django框架,感兴趣的朋友可以了解下
    2020-10-10
  • python MySQLdb Windows下安装教程及问题解决方法

    python MySQLdb Windows下安装教程及问题解决方法

    这篇文章主要介绍了python MySQLdb Windows下安装教程及问题解决方法,本文讲解了安装数据库mysql、安装MySQLdb等步骤,需要的朋友可以参考下
    2015-05-05
  • python对Excel的读取的示例代码

    python对Excel的读取的示例代码

    这篇文章主要介绍了python对Excel的读取的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-02-02
  • Scrapy抓取京东商品、豆瓣电影及代码分享

    Scrapy抓取京东商品、豆瓣电影及代码分享

    Scrapy,Python开发的一个快速、高层次的屏幕抓取和web抓取框架,用于抓取web站点并从页面中提取结构化的数据。Scrapy用途广泛,可以用于数据挖掘、监测和自动化测试。
    2017-11-11
  • 在arcgis使用python脚本进行字段计算时是如何解决中文问题的

    在arcgis使用python脚本进行字段计算时是如何解决中文问题的

    这篇文章主要介绍了在arcgis使用python脚本进行字段计算时是如何解决中文问题的,需要的朋友可以参考下
    2015-10-10

最新评论