|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
以前我做了一个聊天室软件,本来想分享给大家的,结果,没了!!!
没办法,我只能复刻一下这个聊天室软件。
正如标题所说,
我用的是文件来存储信息的( data.pkl ),所以没办法两台电脑聊天……
确实没用!
来看看效果吧:
效果
小缺陷:
- 没给代码标注释,因为懒……
废话挺多的,上代码!
- from copy import deepcopy
- from tkinter import *
- from tkinter import ttk
- from time import strftime
- from tkinter.font import Font
- import os
- import pickle as p
- chat_room = {"Chatroom": []}
- _chat_room = {}
- root = Tk()
- root.geometry("810x670")
- root.title("Chatroom 聊天室")
- user = StringVar()
- room = StringVar()
- _room = ""
- _user = ""
- scroll = None
- def chinese_char_count(s):
- c = 0
- punctuation = [
- "–",
- "—",
- "‘",
- "’",
- "“",
- "”",
- "…",
- "、",
- "。",
- "〈",
- "〉",
- "《",
- "》",
- "「",
- "」",
- "『",
- "』",
- "【",
- "】",
- "〔",
- "〕",
- "!",
- "(",
- ")",
- ",",
- ".",
- ":",
- ";",
- "?",
- ]
- for i in s:
- if "\u4e00" <= i <= "\u9fff" or i in punctuation:
- c += 1
- return c
- def submit():
- v = tk_chat_bar.get("0.0", END)
- tk_chat_bar.delete("0.0", END)
- if not v.isspace():
- if room.get() not in chat_room.keys():
- chat_room[room.get()] = []
- chat_room[room.get()].append(
- (user.get(), strftime("%Y-%m-%d %H:%M:%S"), v[:-1])
- )
- with open(
- os.path.join(os.path.dirname(__file__), "data.pkl"), "wb"
- ) as f:
- p.dump(chat_room, f)
- update()
- def update():
- global chat_room, _chat_room, _room, scroll, _user
- tk_show_chat.config(state=NORMAL)
- with open(os.path.join(os.path.dirname(__file__), "data.pkl"), "rb") as f:
- chat_room = p.load(f)
- if chat_room != _chat_room or _room != room.get() or _user != user.get():
- print(chat_room)
- scroll = round(
- tk_show_chat.yview()[0] * tk_show_chat.get("0.0", END).count("\n")
- )
- tk_show_chat.delete("0.0", END)
- if room.get() in chat_room.keys():
- for i in chat_room[room.get()]:
- tk_show_chat.insert(END, "%s " % (i[1]), "time")
- tk_show_chat.insert(END, "%s " % (i[0]), "user")
- tk_show_chat.insert(END, "说:\n")
- x = 0
- for j in i[2].split("\n"):
- if x < len(j):
- x = len(j) + chinese_char_count(j)
- for j in i[2].split("\n"):
- tk_show_chat.insert(
- END,
- " %s%s "
- % (j, " " * (x - len(j) - chinese_char_count(j))),
- "my_dialog" if i[0] == user.get() else "dialog",
- )
- tk_show_chat.insert(END, "\n")
- tk_show_chat.insert(END, "\n")
- print(scroll)
- tk_show_chat.yview_scroll(scroll, "units")
- _chat_room = deepcopy(chat_room)
- _room = room.get()
- _user = user.get()
- tk_show_chat.config(state=DISABLED)
- def update_aft():
- update()
- root.after(1000, update_aft)
- def original():
- global chat_room
- chat_room = {"Chatroom": []}
- with open(os.path.join(os.path.dirname(__file__), "data.pkl"), "wb") as f:
- p.dump(chat_room, f)
- tk_font = Font(root, family="NSimSun", size=16)
- tk_chat_frame = ttk.Frame(root)
- tk_chat_setting_frame = ttk.Frame(tk_chat_frame)
- tk_user_label = ttk.Label(tk_chat_setting_frame, text="用户名:")
- tk_user_entry = ttk.Entry(tk_chat_setting_frame, textvariable=user)
- tk_room_label = ttk.Label(tk_chat_setting_frame, text="房间:")
- tk_room_entry = ttk.Entry(tk_chat_setting_frame, textvariable=room)
- tk_show_chat_frame = ttk.Frame(tk_chat_frame)
- tk_show_chat_scroll = ttk.Scrollbar(tk_show_chat_frame)
- tk_show_chat = Text(
- tk_show_chat_frame,
- width=70,
- height=25,
- font=tk_font,
- yscrollcommand=tk_show_chat_scroll.set,
- state=DISABLED
- )
- tk_chat_bar_frame = ttk.Frame(tk_chat_frame)
- tk_chat_bar = Text(tk_chat_bar_frame, width=65, height=5, font=tk_font)
- tk_chat_btn = ttk.Button(tk_chat_bar_frame, text="提交", command=submit)
- tk_chat_frame.grid()
- tk_chat_setting_frame.grid(row=0, column=0, sticky=NSEW)
- tk_user_label.pack(side=LEFT)
- tk_user_entry.pack(side=LEFT)
- tk_room_entry.pack(side=RIGHT)
- tk_room_label.pack(side=RIGHT)
- tk_show_chat_frame.grid(row=1, column=0, sticky=NSEW)
- tk_show_chat.pack(side=LEFT, fill=BOTH, expand=True)
- tk_show_chat_scroll.pack(side=LEFT, fill=Y)
- tk_chat_bar_frame.grid(row=2, column=0, sticky=NSEW)
- tk_chat_bar.pack(side=LEFT, fill=BOTH)
- tk_chat_btn.pack(side=LEFT, fill=BOTH, expand=True)
- tk_show_chat_scroll.config(command=tk_show_chat.yview)
- tk_show_chat.tag_config("time", foreground="#888")
- tk_show_chat.tag_config("user", foreground="#33c")
- tk_show_chat.tag_config("dialog", background="#ccc")
- tk_show_chat.tag_config("my_dialog", background="#3c3")
- tk_user_entry.insert(0, "Ckblt")
- tk_room_entry.insert(0, "Chatroom")
- if not os.path.exists(os.path.join(os.path.dirname(__file__), "data.pkl")):
- original()
- update_aft()
- root.mainloop()
复制代码
这个软件我以后会继续完善,有问题可以提出哦~
如果您喜欢这个软件,可以给我评分哦~ |
评分
-
查看全部评分
|