In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 277
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.