diff --git a/README.md b/README.md index e437d6a..4f5f2c3 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ # SimpleChat -一个使用Python开发的简单的聊天室,支持安装插件 +一个简单的聊天软件,支持载入插件 \ No newline at end of file diff --git a/plugins/__init__.py b/plugins/__init__.py index dad063f..b1baab3 100644 --- a/plugins/__init__.py +++ b/plugins/__init__.py @@ -6,28 +6,33 @@ import json from .lib.root import * # TODO: 优化插件管理器UI -# def show_plugin_info(): # 启用的插件信息 - # msg.showinfo("插件信息","插件名: %s\n版本: %s\n作者: %s\n%s"%(enable_name, version, author, description)) - # display("插件列表 版本 作者 简介") - # display("------- ---- ---- ----") - # display("%s %s %s %s"%(name, version, author, description)) -""" -def disable_plugin(): # 禁用插件 - os.rename("plugin\\" + name + ".py", "plugin\\" + name + ".py.disabled") - try: - sys.exit(1) - except SystemExit: - ef.start("run.bat") - os.system("taskkill /f /im pythonw.exe") - -def enable_plugin(): # 启用插件 - os.rename("plugin\\" + disable_name + ".py.disabled", "plugin\\" + disable_name + ".py") - try: - sys.exit(1) - except SystemExit: - ef.start("run.bat") - os.system("taskkill /f /im pythonw.exe") -""" +# +# _oo0oo_ +# o8888888o +# 88" . "88 +# (| -_- |) +# 0\ = /0 +# ___/`---'\___ +# .' \\| |# '. +# / \\||| : |||# \ +# / _||||| -:- |||||- \ +# | | \\\ - #/ | | +# | \_| ''\---/'' |_/ | +# \ .-\__ '-' ___/-. / +# ___'. .' /--.--\ `. .'___ +# ."" '< `.___\_<|>_/___.' >' "". +# | | : `- \`.;`\ _ /`;.`/ - ` : | | +# \ \ `_. \_ __\ /__ _/ .-` / / +# =====`-.____`.___ \_____/___.-`___.-'===== +# `=---=' +# +# +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# +# 佛祖保佑 插件管理器不会出BUG +# +# +# with open(".\\plugins\\plugin_info.json","w",encoding="ansi") as f: # 创建插件json f.write("") for i in os.listdir(".\\plugins"): # 插件文件夹下所有文件 @@ -51,24 +56,30 @@ def enable_plugin(): # 启用插件 f.write(json.dumps(plugin_list, sort_keys = True, separators = (", ", ": "))) f.write("\n") - # _plugin = tk.Menu(find_plugin_menu_enabled, tearoff = False) + _plugin = tk.Menu(find_plugin_menu_enabled, tearoff = False) - # for i in plugin_list: # 遍历插件信息字典 - # name = i + for i in plugin_list: # 遍历插件信息字典 + name = i - # version = plugin_list[name]["version"] # 版本 - # author = plugin_list[name]["author"] # 作者 - # description = plugin_list[name]["description"] # 介绍 + version = plugin_list[name]["version"] # 版本 + author = plugin_list[name]["author"] # 作者 + description = plugin_list[name]["description"] # 介绍 + + def disable_plugin(): # 禁用插件 + global name + os.rename(name, name + ".disabled") - # show_plugin_info() + def enable_plugin(): # 启用插件 + global name + os.rename(name + ".py.disabled", name) - # find_plugin_menu_enabled.add_cascade(label = i, menu = _plugin) - # _plugin.add_command(label = "插件信息", command = show_enable_plugin_info) - # _plugin.add_command(label = "禁用", command = disable_plugin) + find_plugin_menu_enabled.add_cascade(label = i, menu = _plugin) + _plugin.add_command(label = "插件信息", command = lambda: msg.showinfo("插件信息", "作者: %s\n版本: %s\n%s"%(author, version, description))) + _plugin.add_command(label = "禁用", command = disable_plugin) - # elif i[-9:] == ".disabled": # 判断该文件是否为被禁用的插件 - # _plugin = tk.Menu(find_plugin_menu_disabled, tearoff = False) - # disable_name = i.replace(".py.disabled", "") + elif i[-9:] == ".disabled": # 判断该文件是否为被禁用的插件 + _plugin = tk.Menu(find_plugin_menu_disabled, tearoff = False) + disable_name = i.replace(".py.disabled", "") - # find_plugin_menu_disabled.add_cascade(label = disable_name, menu = _plugin) - # _plugin.add_command(label = "启用", command = enable_plugin) \ No newline at end of file + find_plugin_menu_disabled.add_cascade(label = disable_name, menu = _plugin) + _plugin.add_command(label = "启用", command = enable_plugin) \ No newline at end of file diff --git a/plugins/lib/__init__.py b/plugins/lib/__init__.py index b366049..4bdada1 100644 --- a/plugins/lib/__init__.py +++ b/plugins/lib/__init__.py @@ -6,7 +6,7 @@ class Manage: config_menu.add_command(label = "打开插件文件夹", command = open_plugin_dir) # 打开插件文件夹 - config_menu.add_command(label = "载入的插件", command = show_plugin_info) + # config_menu.add_command(label = "载入的插件", command = show_plugin_info) config_menu.add_separator() # 增加分割线 config_menu.add_command(label = "重启", command = restart) # 重启SimpleChat config_menu.add_command(label = "退出", command = eof) # 退出 diff --git a/plugins/lib/functions.py b/plugins/lib/functions.py index 93695a1..ca508d4 100644 --- a/plugins/lib/functions.py +++ b/plugins/lib/functions.py @@ -13,6 +13,8 @@ def restart(): # 重启 try: sys.exit(1) except SystemExit: + os.remove("msg_type.ini") + os.remove("name.ini") ef.start("run.bat") def eof(): # 退出 @@ -21,6 +23,8 @@ def eof(): # 退出 try: sys.exit(1) except SystemExit: + os.remove("msg_type.ini") + os.remove("name.ini") os.system("taskkill /f /im pythonw.exe /t") def open_plugin_dir(): # 打开插件文件夹 diff --git a/plugins/lib/root.py b/plugins/lib/root.py index d5e91c5..01d6c0f 100644 --- a/plugins/lib/root.py +++ b/plugins/lib/root.py @@ -13,15 +13,43 @@ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 创建socket -def display(text, end = "\n"): # 显示消息 +def display(text, end = "\n"): + "向客户端发送消息" text_message.insert(END, text) text_message.insert(END, end) -def send_to_server(text): # 向服务器发送信息 +def send_to_server(text): + "向服务器发送信息" ef.wait(0.1) s.send(bytes(text, "utf-8")) ef.wait(0.1) +def showurl(url): + "显示网址" + url_root = tk.Tk() + + def open_and_quit(): + nonlocal url_root, url + ef.start(url) + url_root.destroy() + + url_root.title("网页链接") + url_root.minsize(300, 100) + + url_yes_or_no_text = tk.Label(url_root, text="你确定要打开以下链接吗?", fg="red") + url_text = tk.Label(url_root, text=url, fg="blue") + url_do_not_open_url_you_dont_trust_text = tk.Label(url_root, text="永远不要打开你不信任的链接", fg="red") + url_yes = ttk.Button(url_root, text="打开它!", command=open_and_quit) + url_no = ttk.Button(url_root, text="关闭此链接", command=url_root.destroy) + + url_yes_or_no_text.grid(row=0, column=1) + url_text.grid(row=1, column=1) + url_do_not_open_url_you_dont_trust_text.grid(row=2, column=1) + url_yes.grid(row=3, column=0) + url_no.grid(row=3, column=2) + + url_root.mainloop() + def send(): # 发送消息 send_msg = text_text.get("0.0", END) s.send(bytes(send_msg, "utf-8")) @@ -50,22 +78,21 @@ def get_msg(): # 将服务器获取到的消息展示到消息面板中 config = ttk.Menubutton(root, text = "管理") # 管理按钮 config_menu = tk.Menu(config, tearoff = False) # 下拉菜单 -# find_plugin_menu = tk.Menu(config_menu, tearoff = False) # 插件显示主菜单 -# find_plugin_menu_enabled = tk.Menu(find_plugin_menu, tearoff = False) # 启用的插件的主菜单 -# find_plugin_menu_disabled = tk.Menu(find_plugin_menu, tearoff = False) # 禁用的插件的主菜单 -""" -for i in os.listdir(".\\plugin"): +find_plugin_menu = tk.Menu(config_menu, tearoff = False) # 插件显示主菜单 +find_plugin_menu_enabled = tk.Menu(find_plugin_menu, tearoff = False) # 启用的插件的主菜单 +find_plugin_menu_disabled = tk.Menu(find_plugin_menu, tearoff = False) # 禁用的插件的主菜单 + +for i in os.listdir(".\\plugins"): if i[-3:] == ".py" or i[-9:] == ".disabled": config_menu.add_cascade(label = "插件管理", menu = find_plugin_menu) # 将插件显示主菜单绑定至管理菜单主菜单 break -for i in os.listdir(".\\plugin"): +for i in os.listdir(".\\plugins"): if i[-3:] == ".py": find_plugin_menu.add_cascade(label = "启用的插件", menu = find_plugin_menu_enabled) # 将启用的插件的主菜单绑定至管理菜单主菜单 break -for i in os.listdir(".\\plugin"): +for i in os.listdir(".\\plugins"): if i[-9:] == ".disabled": find_plugin_menu.add_cascade(label = "禁用的插件", menu = find_plugin_menu_disabled) # 将禁用的插件的主菜单绑定至管理菜单主菜单 break -""" \ No newline at end of file diff --git a/run.bat b/run.bat index fd4867e..043ea16 100644 --- a/run.bat +++ b/run.bat @@ -1,5 +1,5 @@ @echo off -taskkill /im pythonw.exe >nul +taskkill /im pythonw.exe /f >nul pythonw main.pyw -exit +exit \ No newline at end of file diff --git a/server.pyw b/server.pyw index fb8164e..019c6f7 100644 --- a/server.pyw +++ b/server.pyw @@ -31,7 +31,7 @@ def handle_client_in(conn, addr): # 通知谁进入了聊天室 msg_type = f.read() msg = conn.recv(1024) brodcast(msg, ID, msg_type=msg_type) - except Exception as e: + except Exception: conn.close() del client[conn] brodcast(bytes("%s 离开聊天室\n" % ID, "utf-8"), msg_type="system")