python實現(xiàn)簡易聊天對話框
更新時間:2022年02月07日 09:53:20 作者:丶藍色
這篇文章主要為大家詳細介紹了python實現(xiàn)簡易聊天對話框,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python實現(xiàn)簡易聊天對話框的具體代碼,供大家參考,具體內(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()
服務器代碼:
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 = "發(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()
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python實現(xiàn)采用進度條實時顯示處理進度的方法
這篇文章主要介紹了Python實現(xiàn)采用進度條實時顯示處理進度的方法,涉及Python數(shù)學運算結合時間函數(shù)顯示進度效果的相關操作技巧,需要的朋友可以參考下2017-12-12Django全局啟用登陸驗證login_required的方法
這篇文章主要介紹了Django全局啟用登陸驗證login_required的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06python中使用smtplib和email模塊發(fā)送郵件實例
python腳本發(fā)郵件,一般會用到smtplib和email這兩個模塊??纯丛撃K怎么使用,先看smtplib模塊。 smtplib模塊定義了一個簡單的SMTP客戶端,可以用來在互聯(lián)網(wǎng)上發(fā)送郵件2014-04-04django filter過濾器實現(xiàn)顯示某個類型指定字段不同值方式
這篇文章主要介紹了django filter過濾器實現(xiàn)顯示某個類型指定字段不同值方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07