我想有不少小伙伴应该遇到过找文件很麻烦,要特别是文件夹和文件都特别多的情况,不仅有时候会找错文件夹花很长的时间才能找到。桌面快捷方式太多很杂乱、放不下。
现在再也不用担心啦,这一款中文指令运行程序可以很好的解决这一个问题,可以通过这个程序输入中文命令就可以打开你要打开的文件、程序、网页等。
由于是最初版本不是很完善,希望多多包涵。
好的废话不多说了,是时候出来亮亮相了!!!
它长这样
下面是他的python代码
import tkinter as tk
from tkinter import messagebox
import subprocess
import platform
import json
import os
import webbrowser
# 获取桌面路径
if platform.system() == "Windows":
desktop_path = os.path.join(os.path.expanduser("~"), "Desktop")
elif platform.system() == "Darwin":
desktop_path = os.path.join(os.path.expanduser("~"), "Desktop")
elif platform.system() == "Linux":
desktop_path = os.path.join(os.path.expanduser("~"), "Desktop")
# 定义命令映射文件路径
COMMAND_MAPPING_FILE = 'command_mapping.json'
# 尝试从文件中加载命令映射
if os.path.exists(COMMAND_MAPPING_FILE):
with open(COMMAND_MAPPING_FILE, 'r', encoding='utf-8') as f:
command_mapping = json.load(f)
else:
command_mapping = {
"记事本": "notepad.exe" if platform.system() == "Windows" else None,
"计算器": "calc.exe" if platform.system() == "Windows" else
"open -a Calculator" if platform.system() == "Darwin" else
"gnome-calculator" if platform.system() == "Linux" else None,
"微信": "C:\\Program Files (x86)\\Tencent\\WeChat\\WeChat.exe" if platform.system() == "Windows" else
"open -a WeChat" if platform.system() == "Darwin" else None,
"百度": "https://www.baidu.com"
}
def run_command():
input_text = entry.get()
command = command_mapping.get(input_text)
if command:
try:
if platform.system() == "Windows":
if command.startswith("http"):
webbrowser.open(command)
else:
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
subprocess.Popen(command, startupinfo=startupinfo)
elif platform.system() == "Darwin":
if command.startswith("http"):
webbrowser.open(command)
else:
subprocess.Popen(command.split())
elif platform.system() == "Linux":
if command.startswith("http"):
webbrowser.open(command)
else:
subprocess.Popen(command.split())
result_label.config(text="")
except Exception as e:
result_label.config(text=f"执行命令时出错: {e}")
else:
result_label.config(text="未找到对应的命令,请检查输入。")
def add_command():
# 创建添加命令的对话框
add_window = tk.Toplevel(root)
add_window.title("添加命令")
tk.Label(add_window, text="中文命令:").grid(row=0, column=0, padx=10, pady=5)
chinese_entry = tk.Entry(add_window, width=30)
chinese_entry.grid(row=0, column=1, padx=10, pady=5)
tk.Label(add_window, text="计算机命令:").grid(row=1, column=0, padx=10, pady=5)
command_entry = tk.Entry(add_window, width=30)
command_entry.grid(row=1, column=1, padx=10, pady=5)
def save_command():
chinese_command = chinese_entry.get()
computer_command = command_entry.get()
if chinese_command and computer_command:
command_mapping[chinese_command] = computer_command
# 保存命令映射到文件
with open(COMMAND_MAPPING_FILE, 'w', encoding='utf-8') as f:
json.dump(command_mapping, f, ensure_ascii=False, indent=4)
add_window.destroy()
messagebox.showinfo("成功", "命令添加成功!")
result_label.config(text="")
else:
messagebox.showerror("错误", "请输入有效的中文命令和计算机命令。")
tk.Button(add_window, text="保存", command=save_command).grid(row=2, column=0, columnspan=2, pady=10)
def show_commands():
file_path = os.path.join(desktop_path, "命令列表.txt")
try:
with open(file_path, 'w', encoding='utf-8') as f:
for chinese, system_command in command_mapping.items():
f.write(f"中文命令: {chinese}\n系统命令: {system_command}\n\n")
messagebox.showinfo("提示", f"命令列表已保存到 {file_path}")
if platform.system() == "Windows":
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
subprocess.Popen(['notepad.exe', file_path], startupinfo=startupinfo)
elif platform.system() == "Darwin":
subprocess.Popen(['open', file_path])
elif platform.system() == "Linux":
subprocess.Popen(['xdg-open', file_path])
except Exception as e:
messagebox.showerror("错误", f"保存命令列表时出错: {e}")
# 创建主窗口
root = tk.Tk()
root.title("命令执行程序")
root.geometry("600x500") # 进一步调大窗口
# 创建菜单栏
menu_bar = tk.Menu(root)
root.config(menu=menu_bar)
# 创建添加命令的菜单
add_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="添加命令", menu=add_menu)
add_menu.add_command(label="添加新命令", command=add_command)
# 创建查看命令的菜单
view_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="查看命令", menu=view_menu)
view_menu.add_command(label="查看所有命令", command=show_commands)
# 创建一个框架来放置输入框和按钮
input_frame = tk.Frame(root)
input_frame.pack(pady=100, expand=True) # 通过 pady 让其上下居中
# 创建输入框
entry = tk.Entry(input_frame, width=30)
entry.pack(side=tk.LEFT, padx=10)
# 创建按钮
button = tk.Button(input_frame, text="打开", command=run_command)
button.pack(side=tk.LEFT, padx=10)
# 创建结果显示标签
result_label = tk.Label(root, text="")
result_label.pack(pady=10)
# 添加使用方法说明文本
usage_text = "使用方法:\n" \
"1. 在输入框中输入中文命令,如“记事本”、“百度”等,然后点击“打开”按钮执行相应操作。\n" \
"2. 点击“添加命令”菜单中的“添加新命令”,可自定义中文命令和对应的系统命令。\n" \
"3. 点击“查看命令”菜单中的“查看所有命令”,可将命令列表保存到桌面的“命令列表.txt”文件并打开查看。"
usage_label = tk.Label(root, text=usage_text, justify=tk.LEFT, padx=10, pady=10)
usage_label.pack(side=tk.BOTTOM, anchor=tk.W)
# 进入主事件循环
root.mainloop()
大家快试试把,超好用!如有有问题欢迎留言。