網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
本文實(shí)例為大家分享了python實(shí)現(xiàn)簡(jiǎn)易聊天對(duì)話框的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:?
客戶端代碼:?
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 = "發(fā) ?送",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()
服務(wù)器代碼:
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("服務(wù)器") 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 = "發(fā) ?送",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()
原文鏈接:https://blog.csdn.net/lanse_l/article/details/89223106
相關(guān)推薦
- 2022-10-23 C#在Entity?Framework中實(shí)現(xiàn)事務(wù)回滾_C#教程
- 2022-11-27 Git基礎(chǔ)學(xué)習(xí)之標(biāo)簽tag的使用詳解_相關(guān)技巧
- 2022-09-06 python?如何實(shí)現(xiàn)跳過(guò)異常繼續(xù)執(zhí)行_python
- 2022-02-26 Android操作SQLite基本用法_Android
- 2022-07-12 Handler機(jī)制相關(guān)流程
- 2022-10-26 使用Go?http重試請(qǐng)求的示例_Golang
- 2022-11-03 python中for循環(huán)的多種使用實(shí)例_python
- 2023-07-13 替換字符串中的任意字符及正則隱藏手機(jī)號(hào)中間四位
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支