Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use python to realize simple chat dialog box

Shulou Source: shulou.com Published: 2022-06-01 19:25:59 09月22日 Update

This article introduces the relevant knowledge of "how to use python to achieve a simple chat dialog box". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Effect picture:

Client code: 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 ("1024") "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 = 'Microsoft Jacob' # font fontSize = 18 # font size sock = socket.socket (socket.AF_INET,socket.SOCK_STREAM) sock.connect (("127.0.0.1") 8888) mainWindow = tk.Tk () mainWindow.title ("client") mainWindow.minsize (400400) 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 connected\ 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 = "send", 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 ()

Server code:

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 ("1024" "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 = 'Microsoft Jacob' # font fontSize = 18 # font size sock = socket.socket (socket.AF_INET,socket.SOCK_STREAM) sock.bind (("127.0.0.1") 8888) sock.listen (5) mainWindow = tk.Tk () mainWindow.title ("server") mainWindow.minsize (400400) 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 = "send", bg= "orange", fg= "white" Command=lambda:send_func (s) button_send.place (width=100,height=40,x=300,y=360) sMagol addr = sock.accept () t = threading.Thread (target=tcp_recv,args= (s,)) t.start () tk.mainloop () "how to implement simple chat dialogs with python" ends here Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Font utf-8 dialog box simple dialogue code content size customer client more server knowledge Microsoft service practical successful learning next dilemma actual Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Redmi vpn Huawei MySQL