怎么用python实现简易聊天对话框(python,开发技术)

时间:2024-05-02 20:29:13 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

效果图:

怎么用python实现简易聊天对话框

客户端代码:

importtkinterastkfromtkinterimportscrolledtextimportsocketimportthreadingfromdatetimeimportdatetimedeftcp_recv(sock):whileTrue:str=sock.recv(1024).decode("utf-8")show_info(str)defsend_func(sock):str=send_msg.get("0.0","end")sock.send(str.encode("utf-8"))show_info(str)defshow_info(str):now=datetime.now()s_time=now.strftime("%Y-%m-%d%H:%M:%S")str=str.rstrip()iflen(str)==0:return-1send_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()

服务器代码:

importtkinterastkfromtkinterimportscrolledtextimportsocketimportthreadingfromdatetimeimportdatetimedeftcp_recv(sock):whileTrue:str=sock.recv(1024).decode("utf-8")show_info(str)defsend_func(sock):str=send_msg.get("0.0","end")sock.send(str.encode("utf-8"))show_info(str)defshow_info(str):now=datetime.now()s_time=now.strftime("%Y-%m-%d%H:%M:%S")str=str.rstrip()iflen(str)==0:return-1send_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()
 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:怎么用python实现简易聊天对话框的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:javascript的事件驱动机制是什么下一篇:

16 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18