资源简介
基于socket的python聊天,客户端client.py,功能有聊天室群聊、一对一悄悄话私聊等。tcp协议,图形化界面GUI,使用tkinter制作,简单明了
代码片段和文件信息
# -*- coding:utf-8 -*-
#!/usr/bin/python
“Client.py“
import socket
import Tkinter
import Tools
import tkMessageBox
class Client(object):
#初始化函数
def __init__(self):
self.prichat=““ ## privacy chatter
self.v=0 ## privacy choice
self.__login()
#登录界面,输入用户名
#login part
def __login(self):
self.top = Tkinter.Tk()
self.top.title(“login“)
self.top.geometry(“200x100“)
#搭框架,host、port、用户名
#Set up total frame
text = ((“host:“ “localhost“) (“port:“ “6666“) (“name:“ ““))
for i in range(1 len(text)+1):
cmd = compile(“““
frame%d = Tkinter.frame(self.top)
label%d = Tkinter.Label(frame%d text=%rfg=‘blue‘)
label%d.pack(side=Tkinter.LEFT)
self.cwd%d = Tkinter.StringVar(self.top)
self.cwd%d.set(%r)
entry%d = Tkinter.Entry(frame%d width=15 textvariable=self.cwd%d)
entry%d.pack(side=Tkinter.LEFT)
frame%d.pack()“““%(i i i text[i-1][0] i i itext[i-1][1] i i i i i) ““ “exec“)
exec cmd
#登录按钮、退出按钮
#NEW Button:loginclose
btframe = Tkinter.frame(self.topbg=‘Azure‘)
button1 = Tkinter.Button(btframe text=“Login“fg=‘blue‘ bg=‘Azure‘command=self.__check)
button2 = Tkinter.Button(btframe text=“Close“fg=‘blue‘ bg=‘Azure‘command=self.__close)
button1.pack(side=Tkinter.LEFT)
button2.pack(side=Tkinter.LEFT)
btframe.pack()
self.top.mainloop()
#核对信息,无误方可登录
#login check before start
def __check(self ev=None):
host = self.cwd1.get()
if not host: #host error
tkMessageBox.showwarning(“check“ “Host is invalid.“)
return
try: #Client port == Server port
port = int(self.cwd2.get())
except (ValueError TypeError):
tkMessageBox.showwarning(“check“ “Port is invalid.“)
return
self.name = self.cwd3.get() #input user name
if not self.name:
tkMessageBox.showwarning(“check“ “Please write your name.“)
return
connected = False
try: #Connect to server
self.tcpCliSock = socket.socket(socket.AF_INET socket.SOCK_STREAM)
self.tcpCliSock.connect((host port))
self.tcpCliSock.send(‘00|‘+self.name+‘|‘) #Notice:user X login in
connected = True
except:
self.tcpCliSock.close() #if Server is not running
tkMessageBox.showerror(“error“ “Fail to connect to server.“)
self.__close() #quit and delete all
del self.cwd1 self.cwd2 self.cwd3 self.top
if connected:
self.__start()
#聊天界面
#chat part
def __start(self ev=None):
self.locker = Tools.MyLock()
sel
- 上一篇:python之des加密算法
- 下一篇:SCU微服务自动填写
评论
共有 条评论