|
发表于 2023-1-1 16:49:16
|
显示全部楼层
发给你
- from tkinter import *
- import ttkbootstrap as ttk
- from ttkbootstrap.constants 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("微软雅黑 Light", 8)
- chat_text.tag_config("tag_3", font=("MV boli", 9), foreground="grey")
- chat_text.insert(END, time_, "tag_3") # 发送时间
- chat_text.insert(END, "\n")
- #f = Font("微软雅黑 Light", 13)
- chat_text.tag_config("tag_41", font=("微软雅黑 Light", 12), foreground="#808080")
- chat_text.insert(END, username + ":", "tag_41") # 发送文本内容
- chat_text.insert(END, "\n")
-
- #f = Font("微软雅黑 Light", 12)
- chat_text.tag_config("tag_4", font=("微软雅黑 Light", 12), 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("微软雅黑 Light", 10)
- chat_text.tag_config("tag_1", font=("MV Boli", 9), foreground="grey")
- chat_text.insert(END, time_, "tag_1")
- chat_text.insert(END, "\n")
- #f = Font("微软雅黑 Light", 13)
- chat_text.tag_config("tag", font=("微软雅黑 Light", 12), foreground="#808080")
- chat_text.insert(END, res.split(" ", 2)[1], "tag")
- chat_text.insert(END, "\n")
-
- #f = Font("微软雅黑 Light", 12)
- chat_text.tag_config("tag_2", font=("微软雅黑 Light", 12))
- 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=12, width=44, wrap='none', relief=GROOVE, state=DISABLED, font=('微软雅黑 Light', 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=67, height=4) # 发送区域
- send_entry.place(x=20, y=500)
- send_button = ttk.Button(root, text="发送", bootstyle='info-outline', width=5, command=fa_) # 发送按钮
- send_button.place(x=690, y=545)
- send_entry.bind("<Return>", fa_)
- send_entry.bind("<Control-Return>", insert_newline)
- online_user = Listbox(root, height=17)
- online_user.place(x=770, y=30)
- online_user.insert(END, '在线成员列表')
- root.geometry('1000x620')
- 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 = ttk.Window()
- center_window(root, 400, 300) # 实现页面居中
- root.title("Gouzi WD在线聊天室") # 标题
- client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- client.connect(('43.138.0.102', 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 = ttk.Entry(root, bootstyle='primary', textvariable=username_string) #用户名ttk.Entry
- username.place(x=110, y=100)
- Label(root, text="密码:", font=("得意黑", 14)).place(x=10, y=150) # 密码一栏
- password = ttk.Entry(root, bootstyle='primary', textvariable=password_string) # 密码ttk.Entry
- password.place(x=110, y=150)
- username.bind("<Return>", if_login)
- password.bind("<Return>", if_login)
- join_button = ttk.Button(root, text="登陆", bootstyle='warning-outline',command=if_login) # 进入聊天界面按钮
- join_button.place(x=60, y=200)
- res_button = ttk.Button(root, text="注册", bootstyle='success-outline',command=if_res) # 注册
- 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=("微软雅黑 Light", 20)).place(x=130, y=30) # 表头
- # Label(root, text="用户名:", font=("微软雅黑 Light", 14)).place(x=10, y=100) # 用户名一栏
- # username = ttk.Entry(root, textvariable=username_string) #用户名ttk.Entry
- # username.place(x=110, y=100)
- # Label(root, text="ip:", font=("微软雅黑 Light", 14)).place(x=10, y=150) # ip一栏
- # ip = ttk.Entry(root, textvariable=ip_string) #ipttk.Entry
- # ip.place(x=110, y=150)
- # Label(root, text="ip密码(选填):", font=("微软雅黑 Light", 14)).place(x=10, y=200) # 密码一栏
- # password = ttk.Entry(root, textvariable=password_string) # 密码ttk.Entry
- # password.place(x=110, y=200)
- # Label(root, text="预留位:", font=("微软雅黑 Light", 14)).place(x=10, y=250) # 预留一栏
- # password = ttk.Entry(root) # 预留ttk.Entry
- # password.place(x=110, y=250)
- # join_button = ttk.Button(root, text="进入", command=join, font=("微软雅黑 Light", 12)) # 进入聊天界面按钮
- # join_button.place(x=160, y=300)
- # help_button = ttk.Button(root, text="帮助", font=("微软雅黑 Light", 9), command=help) # 帮助按钮
- # help_button.place(x=163, y=350)
- # # join()
- # root.mainloop()
复制代码 |
|