|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 Mike_python小 于 2023-1-26 08:03 编辑
过年了,有压岁钱了 买个服务器重启聊天室
请使用一下客户端登入:
Windows:from tkinter import *
from tkinter.font import Font
# from tkinter.ttk import *
from tkinter import messagebox as mg
import time
import threading
import socket
def center_window(root, width, height):
"""
窗口居中代码
"""
screenwidth = root.winfo_screenwidth() # 获取显示屏宽度
screenheight = root.winfo_screenheight() # 获取显示屏高度
size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2) # 设置窗口居中参数
root.geometry(size) # 让窗口居中显示
def fasong(*args):
time.sleep(0.1)
# re_data = input(">>")
re_data = send_entry.get("1.0", END)[:-1]
if 'exit' == re_data: # 判断是否退出
client.send(("Q "+ username).encode()) # 发送退出的请求
client.close()
elif (re_data != " \n" and re_data != "\n" and re_data != "" and re_data != " "): # 判断内容不为空
client.send(("C " + username + ": " + re_data).encode())
time_ = time.strftime('%Y-%m-%d %H:%M:%S') # 以下为自己说的话
chat_text.config(state=NORMAL)
#f1 = Font("微软雅黑", 8)
chat_text.tag_config("tag_3", font=("微软雅黑", 9), foreground="grey")
chat_text.insert(END, time_, "tag_3") # 发送时间
chat_text.insert(END, "\n")
#f = Font("微软雅黑", 13)
chat_text.tag_config("tag_41", font=("微软雅黑", 14), foreground="#808080")
chat_text.insert(END, username + ":", "tag_41") # 发送文本内容
chat_text.insert(END, "\n")
#f = Font("微软雅黑", 12)
chat_text.tag_config("tag_4", font=("微软雅黑", 13), foreground="#2E8B57")
chat_text.insert(END, re_data, "tag_4") # 发送文本内容
chat_text.insert(END, "\n")
chat_text.config(state=DISABLED)
chat_text.see("end") # 转到最后一行
# send_entry.first()
# send_entry.delete(0.1,2.0) # 设置发送为空
# send_entry.insert('1 wordstart' , "s")
# time.sleep(0.1)
send_entry.delete("0.0", END) # 设置发送为空
def fa_(*args):
fasong()
# send_entry.delete("0.0", END) # 设置发送为空
return 'break'
def jieshou():
while True:
# time.sleep(0.3)
try:
data = client.recv(1024)
res = data.decode()
if(res != ''):
# print("1")
if (res.split(" ", 2)[0]=="C"): # 如果为聊天的请求
# print(res.split(" ", 1)[0])
# print(res.split(" ", 1)[1])
# print(res)
time_ = time.strftime('%Y-%m-%d %H:%M:%S')
chat_text.config(state=NORMAL)
#f1 = Font("微软雅黑", 10)
chat_text.tag_config("tag_1", font=("微软雅黑", 9), foreground="grey")
chat_text.insert(END, time_, "tag_1")
chat_text.insert(END, "\n")
#f = Font("微软雅黑", 13)
chat_text.tag_config("tag", font=("微软雅黑", 14), foreground="#808080")
chat_text.insert(END, res.split(" ", 2)[1], "tag")
chat_text.insert(END, "\n")
#f = Font("微软雅黑", 12)
chat_text.tag_config("tag_2", font=("微软雅黑", 13))
chat_text.insert(END, res.split(" ", 2)[2], "tag_2")
chat_text.insert(END, "\n")
chat_text.config(state=DISABLED)
chat_text.see("end")
elif(res.split(" ", 1)[0]=="R"): # 在线用户请求
afd = False
print(res.split(" ", 1)[1])
for kk in range(0, online_user.size()): # 循环判断在线列表里是否有用户名
if online_user.get(kk) == (res.split(" ", 1)[1]): # 检测到是
afd = True # 判断变量为真
break # 退出
if (afd == False): # 如果判断变量为假
online_user.insert(END,res.split(" ", 1)[1]) # 插入用户名
elif(res.split(" ", 1)[0]=="E"): # 退出请求
for kk in range(0, online_user.size()): # 循环判断要删除的用户名
if online_user.get(kk) == (res.split(" ", 1)[1]):
online_user.delete(kk) # 从在线列表去除
break
# else:
# continue
except Exception as e:
print(e)
print("客户端已退出")
#break
def send():
"""
发送文字
"""
global username
username = username_string.get() # 用户名变量
# time.sleep(3)
if __name__ == "__main__":
# print("Welcome to Gouzi WD Chat")
# print(username)
#注册业务
while True:
# username=input("请输入用户名")
if ' ' not in username and username != " " and username != "": # 判断用户名不为空
client.send(("R "+username + " " + password_string.get()+"\n").encode()) # 发送注册请求
data=client.recv(2048).decode() # 最大接受字节
if data=="OK": # 如果穿回信息为 “OK”
break
else:
mg.showerror("", "用户名不能为空,请强制退出后重新进入") # 用户名如果为空,发出警告
try:
# 创建一个新的线程
new_thread = threading.Thread(target=jieshou, name="T1")
# 启动新线程
new_thread.start()
new_thread1 = threading.Thread(target=fasong, name="T2")
# 启动新线程
new_thread1.start()
except:
client.close()
def insert_newline(*args):
send_entry.insert("insert", "\n")
return "break"
def main():
"""
加入聊天后界面
"""
global chat_text, send_msg, online_user, send_entry
root = Toplevel()
root.title("聊天界面")
center_window(root, 800, 500)
send_msg = StringVar()
"""
——————————————————————————
查收文件text开始
"""
frame = Frame(root)
frame.place(x = 20, y = 20)
gun_song_tiao_y = Scrollbar(frame)
gun_song_tiao_x = Scrollbar(frame, orient=HORIZONTAL)
chat_text = Text(frame, height=14, width=50, wrap='none', relief=GROOVE, state=DISABLED, font=('微软雅黑', 14))#, state=DISABLED
gun_song_tiao_y.pack(side=RIGHT,fill=Y)
gun_song_tiao_x.pack(side=BOTTOM, fill=X)
chat_text.pack()
gun_song_tiao_y.config(command=chat_text.yview)
gun_song_tiao_x.config(command=chat_text.xview)
chat_text.config(yscrollcommand=gun_song_tiao_y.set)
chat_text.config(xscrollcommand=gun_song_tiao_x.set)
"""
查收文件区域text结束
——————————————————————————
"""
send_entry = Text(root, width=70, height=6) # 发送区域
send_entry.place(x=20, y=390)
send_button = Button(root, text="发送", command=fa_) # 发送按钮
send_button.place(x=520, y=390)
send_entry.bind("<Return>", fa_)
send_entry.bind("<Control-Return>", insert_newline)
Label(root, text="在线列表", font=("微软雅黑", 18)).place(x=600, y=20)
online_user = Listbox(root, height=20)
online_user.place(x=600, y=50)
# online_user.insert(END, username_string.get())
def help():
"""
帮助界面
"""
mg.showerror("暂无", "NONE")
def if_login(*args):
main()
send()
def if_res():
pass
def login():
"""注册/登陆"""
global username_string
global username_string, password_string
global client
root = Tk()
center_window(root, 400, 300) # 实现页面居中
root.title("Gouzi WD在线聊天室") # 标题
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('119.3.215.140', 80))
username_string = StringVar() # 存储用户名变量
password_string = StringVar() # 密码存储变量
Label(root, text="登陆界面", font=("微软雅黑", 20)).place(x=130, y=30) # 表头
Label(root, text="用户名:", font=("微软雅黑", 14)).place(x=10, y=100) # 用户名一栏
username = Entry(root, textvariable=username_string) #用户名Entry
username.place(x=110, y=100)
Label(root, text="密码:", font=("微软雅黑", 14)).place(x=10, y=150) # 密码一栏
password = Entry(root, textvariable=password_string) # 密码Entry
password.place(x=110, y=150)
username.bind("<Return>", if_login)
password.bind("<Return>", if_login)
join_button = Button(root, text="登陆", command=if_login, font=("微软雅黑", 12)) # 进入聊天界面按钮
join_button.place(x=60, y=200)
res_button = Button(root, text="注册", command=if_res, font=("微软雅黑", 12)) # 注册
res_button.place(x=230, y=200)
# if_login()
root.mainloop()
login()
# def login():
# """
# 登陆界面,也是初始界面
# """
# root = Tk()
# center_window(root, 400, 500) # 实现页面居中
# root.title("Gouzi WD在线聊天室") # 标题
# username_string = StringVar() # 存储用户名变量
# ip_string = StringVar() # ip存储变量
# password_string = StringVar() # 密码存储变量
# Label(root, text="初始界面", font=("微软雅黑", 20)).place(x=130, y=30) # 表头
# Label(root, text="用户名:", font=("微软雅黑", 14)).place(x=10, y=100) # 用户名一栏
# username = Entry(root, textvariable=username_string) #用户名Entry
# username.place(x=110, y=100)
# Label(root, text="ip:", font=("微软雅黑", 14)).place(x=10, y=150) # ip一栏
# ip = Entry(root, textvariable=ip_string) #ipEntry
# ip.place(x=110, y=150)
# Label(root, text="ip密码(选填):", font=("微软雅黑", 14)).place(x=10, y=200) # 密码一栏
# password = Entry(root, textvariable=password_string) # 密码Entry
# password.place(x=110, y=200)
# Label(root, text="预留位:", font=("微软雅黑", 14)).place(x=10, y=250) # 预留一栏
# password = Entry(root) # 预留Entry
# password.place(x=110, y=250)
# join_button = Button(root, text="进入", command=join, font=("微软雅黑", 12)) # 进入聊天界面按钮
# join_button.place(x=160, y=300)
# help_button = Button(root, text="帮助", font=("微软雅黑", 9), command=help) # 帮助按钮
# help_button.place(x=163, y=350)
# # join()
# root.mainloop()
Mac版本:from tkinter import *
from tkinter.font import Font
# from tkinter.ttk import *
from tkinter import messagebox as mg
import time
import threading
import socket
# from socket import SIO_KEEPALIVE_VALS
def center_window(root, width, height):
"""
窗口居中代码
"""
screenwidth = root.winfo_screenwidth() # 获取显示屏宽度
screenheight = root.winfo_screenheight() # 获取显示屏高度
size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2) # 设置窗口居中参数
root.geometry(size) # 让窗口居中显示
def fasong(*args):
time.sleep(0.1)
# re_data = input(">>")
re_data = send_entry.get("1.0", END)[:-1]
if 'exit' == re_data: # 判断是否退出
client.send(("Q "+ username).encode()) # 发送退出的请求
client.close()
elif (re_data != " \n" and re_data != "\n" and re_data != "" and re_data != " "): # 判断内容不为空
client.send(("C " + username + ": " + re_data).encode())
time_ = time.strftime('%Y-%m-%d %H:%M:%S') # 以下为自己说的话
chat_text.config(state=NORMAL)
f1 = Font(size=10)
chat_text.tag_config("tag_3", font=f1, foreground="grey")
chat_text.insert(END, time_, "tag_3") # 发送时间
chat_text.insert(END, "\n")
f = Font(size=15)
chat_text.tag_config("tag_41", font=f, foreground="#808080")
chat_text.insert(END, username + ":", "tag_41") # 发送文本内容
chat_text.insert(END, "\n")
f = Font(size=14)
chat_text.tag_config("tag_4", font=f, foreground="#2E8B57")
chat_text.insert(END, re_data, "tag_4") # 发送文本内容
chat_text.insert(END, "\n")
chat_text.config(state=DISABLED)
chat_text.see("end") # 转到最后一行
# send_entry.first()
# send_entry.delete(0.1,2.0) # 设置发送为空
# send_entry.insert('1 wordstart' , "s")
# time.sleep(0.1)
send_entry.delete("0.0", END) # 设置发送为空
def fa_(*args):
fasong()
# send_entry.delete("0.0", END) # 设置发送为空
return 'break'
def jieshou():
while True:
# time.sleep(0.3)
try:
data = client.recv(1024)
res = data.decode()
if(res != ''):
# print("1")
if (res.split(" ", 2)[0]=="C"): # 如果为聊天的请求
# print(res.split(" ", 1)[0])
# print(res.split(" ", 1)[1])
# print(res)
time_ = time.strftime('%Y-%m-%d %H:%M:%S')
chat_text.config(state=NORMAL)
f1 = Font(size=10)
chat_text.tag_config("tag_1", font=f1, foreground="grey")
chat_text.insert(END, time_, "tag_1")
chat_text.insert(END, "\n")
f = Font(size=15)
chat_text.tag_config("tag", font=f, foreground="#808080")
chat_text.insert(END, res.split(" ", 2)[1], "tag")
chat_text.insert(END, "\n")
f = Font(size=14)
chat_text.tag_config("tag_2", font=f)
chat_text.insert(END, res.split(" ", 2)[2], "tag_2")
chat_text.insert(END, "\n")
chat_text.config(state=DISABLED)
chat_text.see("end")
elif(res.split(" ", 1)[0]=="R"): # 在线用户请求
afd = False
print(res.split(" ", 1)[1])
for kk in range(0, online_user.size()): # 循环判断在线列表里是否有用户名
if online_user.get(kk) == (res.split(" ", 1)[1]): # 检测到是
afd = True # 判断变量为真
break # 退出
if (afd == False): # 如果判断变量为假
online_user.insert(END,res.split(" ", 1)[1]) # 插入用户名
elif(res.split(" ", 1)[0]=="E"): # 退出请求
for kk in range(0, online_user.size()): # 循环判断要删除的用户名
if online_user.get(kk) == (res.split(" ", 1)[1]):
online_user.delete(kk) # 从在线列表去除
break
# else:
# continue
except Exception as e:
print(e)
print("客户端已退出")
#break
def send():
"""
发送文字
"""
global username
username = username_string.get() # 用户名变量
# time.sleep(3)
if __name__ == "__main__":
# print("Welcome to Gouzi WD Chat")
# print(username)
#注册业务
while True:
# username=input("请输入用户名")
if ' ' not in username and username != " " and username != "": # 判断用户名不为空
client.send(("R "+username + " " + password_string.get()+"\n").encode()) # 发送注册请求
data=client.recv(2048).decode() # 最大接受字节
if data=="OK": # 如果穿回信息为 “OK”
break
else:
mg.showerror("", "用户名不能为空,请强制退出后重新进入") # 用户名如果为空,发出警告
try:
# 创建一个新的线程
new_thread = threading.Thread(target=jieshou, name="T1")
# 启动新线程
new_thread.start()
new_thread1 = threading.Thread(target=fasong, name="T2")
# 启动新线程
new_thread1.start()
except:
client.close()
def insert_newline(*args):
send_entry.insert("insert", "\n")
return "break"
def main():
"""
加入聊天后界面
"""
global chat_text, send_msg, online_user, send_entry
root = Toplevel()
root.title("聊天界面")
center_window(root, 800, 500)
send_msg = StringVar()
"""
——————————————————————————
查收文件text开始
"""
frame = Frame(root)
frame.place(x = 20, y = 20)
gun_song_tiao_y = Scrollbar(frame)
gun_song_tiao_x = Scrollbar(frame, orient=HORIZONTAL)
chat_text = Text(frame, height=20, width=60, wrap='none', relief=GROOVE, state=DISABLED, font=('微软雅黑', 14))#, state=DISABLED
gun_song_tiao_y.pack(side=RIGHT,fill=Y)
gun_song_tiao_x.pack(side=BOTTOM, fill=X)
chat_text.pack()
gun_song_tiao_y.config(command=chat_text.yview)
gun_song_tiao_x.config(command=chat_text.xview)
chat_text.config(yscrollcommand=gun_song_tiao_y.set)
chat_text.config(xscrollcommand=gun_song_tiao_x.set)
"""
查收文件区域text结束
——————————————————————————
"""
send_entry = Text(root, width=70, height=6) # 发送区域
send_entry.place(x=20, y=390)
send_button = Button(root, text="发送", command=fa_) # 发送按钮
send_button.place(x=520, y=390)
send_entry.bind("<Return>", fa_)
send_entry.bind("<Control-Return>", insert_newline)
Label(root, text="在线列表", font=("微软雅黑", 18)).place(x=600, y=20)
online_user = Listbox(root, height=25)
online_user.place(x=600, y=50)
# online_user.insert(END, username_string.get())
def help():
"""
帮助界面
"""
mg.showerror("暂无", "NONE")
def if_login(*args):
main()
send()
def if_res():
pass
def login():
"""注册/登陆"""
global username_string
global username_string, password_string
global client
root = Tk()
center_window(root, 400, 300) # 实现页面居中
root.title("Gouzi WD在线聊天室") # 标题
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('119.3.215.140', 80))
# client.setsockopt(socket.SOL_SOCKET, socket.SOCK_STREAM, True) # windows写法
# client.ioctl(socket.SIO_KEEPALIVE_VALS, (1, 60*1000, 30*1000)) # windows写法
client.setsockopt(socket.SOL_SOCKET,socket.SO_KEEPALIVE,True)
#self.my_socket.ioctl(SIO_KEEPALIVE_VALS,(1,10000,1000))
client.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPALIVE, 10)#
client.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 3)
client.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 5)
username_string = StringVar() # 存储用户名变量
password_string = StringVar() # 密码存储变量
Label(root, text="登陆界面", font=("微软雅黑", 20)).place(x=130, y=30) # 表头
Label(root, text="用户名:", font=("微软雅黑", 14)).place(x=10, y=100) # 用户名一栏
username = Entry(root, textvariable=username_string) #用户名Entry
username.place(x=110, y=100)
Label(root, text="密码:", font=("微软雅黑", 14)).place(x=10, y=150) # 密码一栏
password = Entry(root, textvariable=password_string, show="*") # 密码Entry
password.place(x=110, y=150)
username.bind("<Return>", if_login)
password.bind("<Return>", if_login)
join_button = Button(root, text="登陆", command=if_login, font=("微软雅黑", 12)) # 进入聊天界面按钮
join_button.place(x=60, y=200)
res_button = Button(root, text="注册", command=if_res, font=("微软雅黑", 12)) # 注册
res_button.place(x=230, y=200)
# if_login()
root.mainloop()
login()
# def login():
# """
# 登陆界面,也是初始界面
# """
# root = Tk()
# center_window(root, 400, 500) # 实现页面居中
# root.title("Gouzi WD在线聊天室") # 标题
# username_string = StringVar() # 存储用户名变量
# ip_string = StringVar() # ip存储变量
# password_string = StringVar() # 密码存储变量
# Label(root, text="初始界面", font=("微软雅黑", 20)).place(x=130, y=30) # 表头
# Label(root, text="用户名:", font=("微软雅黑", 14)).place(x=10, y=100) # 用户名一栏
# username = Entry(root, textvariable=username_string) #用户名Entry
# username.place(x=110, y=100)
# Label(root, text="ip:", font=("微软雅黑", 14)).place(x=10, y=150) # ip一栏
# ip = Entry(root, textvariable=ip_string) #ipEntry
# ip.place(x=110, y=150)
# Label(root, text="ip密码(选填):", font=("微软雅黑", 14)).place(x=10, y=200) # 密码一栏
# password = Entry(root, textvariable=password_string) # 密码Entry
# password.place(x=110, y=200)
# Label(root, text="预留位:", font=("微软雅黑", 14)).place(x=10, y=250) # 预留一栏
# password = Entry(root) # 预留Entry
# password.place(x=110, y=250)
# join_button = Button(root, text="进入", command=join, font=("微软雅黑", 12)) # 进入聊天界面按钮
# join_button.place(x=160, y=300)
# help_button = Button(root, text="帮助", font=("微软雅黑", 9), command=help) # 帮助按钮
# help_button.place(x=163, y=350)
# # join()
# root.mainloop()
|
评分
-
查看全部评分
|