鱼C论坛

 找回密码
 立即注册
查看: 2292|回复: 17

[作品展示] 【Python】聊天室!但好像没啥用……

[复制链接]
发表于 2022-2-12 17:44:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
以前我做了一个聊天室软件,本来想分享给大家的,结果,没了!!!
没办法,我只能复刻一下这个聊天室软件

正如标题所说,
我用的是文件来存储信息的( data.pkl ),所以没办法两台电脑聊天……
确实没用!

来看看效果吧:

效果

效果


小缺陷:
  • 没给代码标注释,因为懒……


废话挺多的,上代码!
  1. from copy import deepcopy
  2. from tkinter import *
  3. from tkinter import ttk
  4. from time import strftime
  5. from tkinter.font import Font
  6. import os
  7. import pickle as p


  8. chat_room = {"Chatroom": []}
  9. _chat_room = {}

  10. root = Tk()
  11. root.geometry("810x670")
  12. root.title("Chatroom 聊天室")

  13. user = StringVar()
  14. room = StringVar()
  15. _room = ""
  16. _user = ""

  17. scroll = None


  18. def chinese_char_count(s):
  19.     c = 0
  20.     punctuation = [
  21.         "–",
  22.         "—",
  23.         "‘",
  24.         "’",
  25.         "“",
  26.         "”",
  27.         "…",
  28.         "、",
  29.         "。",
  30.         "〈",
  31.         "〉",
  32.         "《",
  33.         "》",
  34.         "「",
  35.         "」",
  36.         "『",
  37.         "』",
  38.         "【",
  39.         "】",
  40.         "〔",
  41.         "〕",
  42.         "!",
  43.         "(",
  44.         ")",
  45.         ",",
  46.         ".",
  47.         ":",
  48.         ";",
  49.         "?",
  50.     ]

  51.     for i in s:
  52.         if "\u4e00" <= i <= "\u9fff" or i in punctuation:
  53.             c += 1
  54.     return c


  55. def submit():
  56.     v = tk_chat_bar.get("0.0", END)
  57.     tk_chat_bar.delete("0.0", END)
  58.     if not v.isspace():
  59.         if room.get() not in chat_room.keys():
  60.             chat_room[room.get()] = []

  61.         chat_room[room.get()].append(
  62.             (user.get(), strftime("%Y-%m-%d %H:%M:%S"), v[:-1])
  63.         )

  64.         with open(
  65.             os.path.join(os.path.dirname(__file__), "data.pkl"), "wb"
  66.         ) as f:
  67.             p.dump(chat_room, f)

  68.         update()


  69. def update():
  70.     global chat_room, _chat_room, _room, scroll, _user

  71.     tk_show_chat.config(state=NORMAL)

  72.     with open(os.path.join(os.path.dirname(__file__), "data.pkl"), "rb") as f:
  73.         chat_room = p.load(f)

  74.     if chat_room != _chat_room or _room != room.get() or _user != user.get():
  75.         print(chat_room)
  76.         scroll = round(
  77.             tk_show_chat.yview()[0] * tk_show_chat.get("0.0", END).count("\n")
  78.         )

  79.         tk_show_chat.delete("0.0", END)
  80.         if room.get() in chat_room.keys():
  81.             for i in chat_room[room.get()]:
  82.                 tk_show_chat.insert(END, "%s " % (i[1]), "time")
  83.                 tk_show_chat.insert(END, "%s " % (i[0]), "user")
  84.                 tk_show_chat.insert(END, "说:\n")
  85.                 x = 0
  86.                 for j in i[2].split("\n"):
  87.                     if x < len(j):
  88.                         x = len(j) + chinese_char_count(j)
  89.                 for j in i[2].split("\n"):

  90.                     tk_show_chat.insert(
  91.                         END,
  92.                         " %s%s "
  93.                         % (j, " " * (x - len(j) - chinese_char_count(j))),
  94.                         "my_dialog" if i[0] == user.get() else "dialog",
  95.                     )
  96.                     tk_show_chat.insert(END, "\n")

  97.                 tk_show_chat.insert(END, "\n")
  98.         print(scroll)
  99.         tk_show_chat.yview_scroll(scroll, "units")
  100.         _chat_room = deepcopy(chat_room)
  101.         _room = room.get()
  102.         _user = user.get()

  103.     tk_show_chat.config(state=DISABLED)


  104. def update_aft():
  105.     update()
  106.     root.after(1000, update_aft)


  107. def original():
  108.     global chat_room
  109.     chat_room = {"Chatroom": []}
  110.     with open(os.path.join(os.path.dirname(__file__), "data.pkl"), "wb") as f:
  111.         p.dump(chat_room, f)


  112. tk_font = Font(root, family="NSimSun", size=16)


  113. tk_chat_frame = ttk.Frame(root)
  114. tk_chat_setting_frame = ttk.Frame(tk_chat_frame)
  115. tk_user_label = ttk.Label(tk_chat_setting_frame, text="用户名:")
  116. tk_user_entry = ttk.Entry(tk_chat_setting_frame, textvariable=user)
  117. tk_room_label = ttk.Label(tk_chat_setting_frame, text="房间:")
  118. tk_room_entry = ttk.Entry(tk_chat_setting_frame, textvariable=room)
  119. tk_show_chat_frame = ttk.Frame(tk_chat_frame)
  120. tk_show_chat_scroll = ttk.Scrollbar(tk_show_chat_frame)
  121. tk_show_chat = Text(
  122.     tk_show_chat_frame,
  123.     width=70,
  124.     height=25,
  125.     font=tk_font,
  126.     yscrollcommand=tk_show_chat_scroll.set,
  127.     state=DISABLED
  128. )
  129. tk_chat_bar_frame = ttk.Frame(tk_chat_frame)
  130. tk_chat_bar = Text(tk_chat_bar_frame, width=65, height=5, font=tk_font)
  131. tk_chat_btn = ttk.Button(tk_chat_bar_frame, text="提交", command=submit)


  132. tk_chat_frame.grid()

  133. tk_chat_setting_frame.grid(row=0, column=0, sticky=NSEW)
  134. tk_user_label.pack(side=LEFT)
  135. tk_user_entry.pack(side=LEFT)
  136. tk_room_entry.pack(side=RIGHT)
  137. tk_room_label.pack(side=RIGHT)

  138. tk_show_chat_frame.grid(row=1, column=0, sticky=NSEW)
  139. tk_show_chat.pack(side=LEFT, fill=BOTH, expand=True)
  140. tk_show_chat_scroll.pack(side=LEFT, fill=Y)

  141. tk_chat_bar_frame.grid(row=2, column=0, sticky=NSEW)
  142. tk_chat_bar.pack(side=LEFT, fill=BOTH)
  143. tk_chat_btn.pack(side=LEFT, fill=BOTH, expand=True)


  144. tk_show_chat_scroll.config(command=tk_show_chat.yview)


  145. tk_show_chat.tag_config("time", foreground="#888")
  146. tk_show_chat.tag_config("user", foreground="#33c")
  147. tk_show_chat.tag_config("dialog", background="#ccc")
  148. tk_show_chat.tag_config("my_dialog", background="#3c3")


  149. tk_user_entry.insert(0, "Ckblt")
  150. tk_room_entry.insert(0, "Chatroom")


  151. if not os.path.exists(os.path.join(os.path.dirname(__file__), "data.pkl")):
  152.     original()


  153. update_aft()
  154. root.mainloop()
复制代码


这个软件我以后会继续完善,有问题可以提出哦~
如果您喜欢这个软件,可以给我评分哦~

评分

参与人数 4荣誉 +4 鱼币 +7 收起 理由
shiyouroc + 1 无条件支持楼主!
小伤口 + 2 + 2 鱼C有你更精彩^_^
1molHF + 1 鱼C有你更精彩^_^
python爱好者. + 5 感谢楼主无私奉献!

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-2-12 19:04:15 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-2-12 22:05:17 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-2-13 10:55:33 | 显示全部楼层
大神,接受我的膝盖
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-13 19:15:30 | 显示全部楼层
厉害,加油
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-28 08:19:13 From FishC Mobile | 显示全部楼层
666
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-2-28 09:57:15 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-2-28 16:19:09 | 显示全部楼层
这个我怎么感觉就是文件同步....一个程序发言写入文件,其他程序读取文件
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-2-28 17:58:33 | 显示全部楼层
yuanming1027 发表于 2022-2-28 16:19
这个我怎么感觉就是文件同步....一个程序发言写入文件,其他程序读取文件

差不多
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-28 18:31:24 | 显示全部楼层
牛啊
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-6 10:47:06 From FishC Mobile | 显示全部楼层
用pyqt不是更好吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-6 11:07:01 | 显示全部楼层
沮授 发表于 2022-3-6 10:47
用pyqt不是更好吗?

暂时还不会
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-12 19:29:24 From FishC Mobile | 显示全部楼层
正好我之前也做过一个,没弄完
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-12 19:32:48 From FishC Mobile | 显示全部楼层
我之前做的是用的socket,UDP协议,加了个端口号,本机信息显示,测试网络连通性什么的,还有分组,但网络信息加密和校验还没做好,不过你那个tag我可以学学
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-8 17:55:58 | 显示全部楼层
嘿,伙计,我写的那个把验证部分删掉了,四舍五入就是完成了,邮箱发给你看看?(打包成exe和安装包了)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-8 18:01:10 | 显示全部楼层
Minecraft程序猿 发表于 2022-4-8 17:55
嘿,伙计,我写的那个把验证部分删掉了,四舍五入就是完成了,邮箱发给你看看?(打包成exe和安装包了)

好的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-8 18:40:41 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-9 14:57:51 From FishC Mobile | 显示全部楼层
其实现在主流的网络通信方式socket也是采用了文件读写的思想
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-29 09:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表