udpServer.py #接收所有客户端的来信,并对登录用户推送消息
from socket import *
import threading
host = '' #监听所有的ip
port = 13141 #接口必须一致
bufsize = 1024
addrlist = list()
addr = (host,port)
udpServer = socket(AF_INET,SOCK_DGRAM)
udpServer.bind(addr) #绑定地址
while True:
data,clientaddr = udpServer.recvfrom(bufsize) #接收数据 自动阻塞 等待客户端请求:
data = data.decode(encoding='utf-8')
print('客户端的来信:',data,clientaddr)
if clientaddr not in addrlist:
addrlist.append(clientaddr)
for i in addrlist:
udpServer.sendto(data.encode(encoding='utf-8'),i)
udpServer.close()
main.py
from tkinter import *
from LoginPage import *
root = Tk()
root.title('聊天室')
LoginPage(root)
root.mainloop()
LoginPage.py
from tkinter import *
from tkinter.messagebox import *
from MainPage import *
class LoginPage(object):
def __init__(self, master=None):
self.root = master #定义内部变量root
self.root.geometry("300x180") #设置窗口大小
self.username = StringVar()
self.password = StringVar()
self.createPage()
def createPage(self):
self.page = Frame(self.root) #创建Frame
self.page.pack()
Label(self.page).grid(row=0, stick=W)
Label(self.page, text = '账户: ').grid(row=1, stick=W, pady=10)
Entry(self.page, textvariable=self.username).grid(row=1, column=1, stick=E)
Label(self.page, text = '密码: ').grid(row=2, stick=W, pady=10)
Entry(self.page, textvariable=self.password, show='*').grid(row=2, column=1, stick=E)
Button(self.page, text='登陆', command=self.loginCheck).grid(row=3, stick=W, pady=10)
Button(self.page, text='退出', command=self.page.quit).grid(row=3, column=1, stick=E)
def loginCheck(self):
name = self.username.get()
secret = self.password.get()
if secret=='123456':
self.page.destroy()
mypage = MainPage(self.root,name)
thread1 = myThread(1,mypage)
thread1.start()
else:
showinfo(title='错误', message='账号或密码错误!')
MainPage.py #布局可以自己调整
from tkinter import * #导入TKinter模块
from socket import *
import threading
class MainPage(object):
def __init__(self,master=None,name):
self.name = name
self.root = master #定义内部变量root
self.root.geometry("800x600") #设置窗口尺寸
self.bufsize = 1024 #定义缓冲大小
self.serveraddr = ('127.0.0.1',13141) # 服务器ip,端口,元祖形式
self.udpClient = socket(AF_INET,SOCK_DGRAM) #创建客户端
self.content = list()
self.createPage()
def createPage(self):
self.frame1=Frame(self.root)
self.frame2=Frame(self.root)
self.frame1.pack()
self.frame2.pack(side=BOTTOM)
self.inp_text=Entry(self.frame2,width=60) #创建输入框(输入聊天内容)
self.btn = Button(self.frame2,text="发送",width=15,command=self.sendmsg) #绑定获取文本框内容方法
self.inp_text.pack(side=LEFT)
self.btn.pack(side=LEFT)
def sendmsg(self):
self.get_inp = self.inp_text.get() #获取输入框内容
if self.get_inp:
self.get_inp = self.name+':' + self.get_inp #添加姓名标示
self.get_inp = self.get_inp.encode(encoding="utf-8")
self.udpClient.sendto(self.get_inp,self.serveraddr) # 发送数据
self.inp_text.delete('0','end')
def recvmsg(self):
self.data,self.addr = self.udpClient.recvfrom(self.bufsize) #接收数据和返回地址
self.data = self.data.decode(encoding="utf-8")
# self.content.append(self.data+'\n')
# self.ss=''.join(self.content)
return self.data
class myThread(threading.Thread):
def __init__(self,id,mypage = None):
self.mypage = mypage #获取MainPage的实例化对象
threading.Thread.__init__(self)
self.id=id
def run(self):
if self.id == 1:
while True:
self.ss = self.mypage.recvmsg()
if self.ss:
print(self.ss)
self.mypage.text=Label(self.mypage.frame1,text=self.ss) #创建聊天内容标签
self.mypage.text.pack(side=TOP,padx=1)