Skip to content

优化插件管理器UI #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# SimpleChat
一个使用Python开发的简单的聊天室,支持安装插件
一个简单的聊天软件,支持载入插件
85 changes: 48 additions & 37 deletions plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"): # 插件文件夹下所有文件
Expand All @@ -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)
find_plugin_menu_disabled.add_cascade(label = disable_name, menu = _plugin)
_plugin.add_command(label = "启用", command = enable_plugin)
2 changes: 1 addition & 1 deletion plugins/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) # 退出
Expand Down
4 changes: 4 additions & 0 deletions plugins/lib/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(): # 退出
Expand All @@ -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(): # 打开插件文件夹
Expand Down
47 changes: 37 additions & 10 deletions plugins/lib/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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
"""
4 changes: 2 additions & 2 deletions run.bat
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@echo off

taskkill /im pythonw.exe >nul
taskkill /im pythonw.exe /f >nul
pythonw main.pyw
exit
exit
2 changes: 1 addition & 1 deletion server.pyw
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down