English 简体中文 繁體中文 한국 사람 日本語 Deutsch русский بالعربية TÜRKÇE português คนไทย french
查看: 14|回复: 0

Windows 快速启动器

[复制链接]
查看: 14|回复: 0

Windows 快速启动器

[复制链接]
查看: 14|回复: 0

399

主题

0

回帖

1207

积分

金牌会员

积分
1207
suzai

399

主题

0

回帖

1207

积分

金牌会员

积分
1207
2025-2-5 19:44:23 | 显示全部楼层 |阅读模式
在日常工作和学习中,我们经常需要快速访问某些功能或资源,例如打开常用网站、启动应用程序或执行特定命令。Linux 用户可以通过 alias 快速实现这些操作,但在 Windows 上缺乏类似的原生支持。本文将介绍如何使用 Python 开发一个 隐藏式文本输入框程序,模拟 Linux 的 alias 功能,实现快速启动和访问。
<hr>功能概述

该程序的核心功能如下:

  • 隐藏式界面:默认情况下,程序界面隐藏,按下热键后显示。
  • 快速输入:通过键盘输入关键字,按下回车键后执行相应操作。
  • 自定义映射:通过配置文件(如 store.txt)定义关键字与操作的映射关系。
  • 快速打开网站:输入关键字后,自动打开对应的 URL。
  • 打开文件夹:输入关键字后,打开指定的文件夹。
  • 启动应用程序:输入关键字后,启动指定的应用程序。
  • 执行系统命令:输入关键字后,执行系统命令(如打开服务管理器)。
  • 单实例运行:确保程序只运行一个实例,避免重复启动。
<hr>实现原理

1. 隐藏式界面

使用 tkinter 创建一个无标题栏的文本输入框界面,默认隐藏。通过全局热键(如 Alt + Win + Z)显示界面,并将焦点设置到输入框。
2. 关键字映射

通过读取配置文件(如 store.txt),将关键字与操作(如 URL、文件夹路径、应用程序路径或系统命令)关联起来。例如:
 
google https://www.google.com upython https://www.python.org uubunt D:\ubunt fservices %SystemRoot%\system32\services.msc onotepad C:\Windows\System32\notepad.exe o
 





3. 快速执行

输入关键字后,按下回车键,程序会根据配置文件执行相应操作(如打开浏览器访问 URL、打开文件夹、启动应用程序或执行系统命令)。
4. 单实例运行

使用 psutil 检查程序是否已经运行,避免重复启动。
<hr>代码实现

以下是完整的 Python 代码:
python
复制



import tkinter as tkimport keyboardimport ctypesimport sysimport osimport psutilimport webbrowserimport subprocessimport timeclass StoreItem:    def __init__(self, keyword, content, type_):        self.keyword = keyword        self.content = content        self.type_ = type_class Store:    def __init__(self, file_path):        self.file_path = file_path        self.items = {}        self.last_modified = 0        self.load()    def load(self):        if not os.path.exists(self.file_path):            print(f"错误:未找到文件 {self.file_path},请确保 store.txt 和脚本在同一目录下。")            return        self.last_modified = os.path.getmtime(self.file_path)        self.items.clear()        with open(self.file_path, "r", encoding="utf-8") as file:            for line in file:                columns = line.strip().split()                if len(columns) == 3:                    keyword, content, type_ = columns                    self.items[keyword] = StoreItem(keyword, content, type_)    def get_item(self, keyword):        if os.path.getmtime(self.file_path) > self.last_modified:            self.load()        return self.items.get(keyword)def is_already_running():    current_pid = os.getpid()    for proc in psutil.process_iter(['pid', 'name']):        try:            if proc.info['name'] == os.path.basename(sys.argv[0]) and proc.info['pid'] != current_pid:                return True        except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):            pass    return Falseif is_already_running():    print("程序已经在运行中,退出当前实例。")    sys.exit(0)user32 = ctypes.windll.user32imm32 = ctypes.windll.imm32def show_window():    if not root.winfo_viewable():        root.deiconify()    root.lift()    root.focus_force()    screen_width = root.winfo_screenwidth()    screen_height = root.winfo_screenheight()    window_width = root.winfo_reqwidth()    window_height = root.winfo_reqheight()    x = screen_width - window_width - 20    y = 20    root.geometry(f"+{x}+{y}")    text_entry.focus_set()    text_entry.delete(0, tk.END)    switch_to_english()def hide_window():    root.withdraw()def on_enter(event):    input_text = text_entry.get()    if input_text.strip():        ontext(input_text)    text_entry.delete(0, tk.END)    hide_window()def handle_action(item):    if item.type_ == "u":        webbrowser.open(item.content)        print(f"已打开 URL: {item.content}")    elif item.type_ == "f":        os.startfile(item.content)        print(f"已打开文件夹: {item.content}")    elif item.type_ == "o":        subprocess.run(item.content, shell=True)        print(f"已执行命令: {item.content}")def ontext(key):    item = store.get_item(key)    if item:        handle_action(item)    else:        print(f"未找到关键字为 {key} 的记录。")def switch_to_english():    hwnd = user32.GetForegroundWindow()    himc = imm32.ImmGetContext(hwnd)    if himc:        imm32.ImmSetOpenStatus(himc, False)        imm32.ImmReleaseContext(hwnd, himc)root = tk.Tk()root.overrideredirect(True)root.configure(bg="white")text_entry = tk.Entry(    root,    width=20,    font=("Arial", 12),    bd=0,    highlightthickness=1,    justify="left")text_entry.pack(padx=10, pady=10)root.withdraw()text_entry.bind("<Return>", on_enter)keyboard.add_hotkey("alt+windows+z", show_window)keyboard.add_hotkey("esc", hide_window)text_entry.bind("<FocusIn>", lambda event: switch_to_english())script_dir = os.path.dirname(os.path.abspath(__file__))store_path = os.path.join(script_dir, "store.txt")store = Store(store_path)root.mainloop()
 

<hr>配置文件示例

在 store.txt 文件中添加以下内容:
 google https://www.google.com upython https://www.python.org uubunt D:\ubunt fservices %SystemRoot%\system32\services.msc onotepad C:\Windows\System32\notepad.exe o
 

 




<hr>功能说明


  • 打开 URL

    • 输入 google,打开 https://www.google.com。
    • 输入 python,打开 https://www.python.org。

  • 打开文件夹

    • 输入 ubunt,打开 D:\ubunt 文件夹。

  • 启动应用程序或执行系统命令

    • 输入 services,打开服务管理器。
    • 输入 notepad,启动记事本。

<hr>使用方法


  • 将 Python 脚本和 store.txt 文件放在同一目录下。
  • 运行 Python 脚本。
  • 按下 Alt + Win + Z 显示界面,输入关键字后按 Enter 键。
  • 程序会根据配置文件执行相应操作。
<hr>打包为可执行文件

使用 PyInstaller 将脚本打包为 Windows 可执行文件:
bash
复制



pyinstaller --onefile --windowed main.py
生成的 main.exe 文件位于 dist 文件夹中。
<hr>总结

通过抽象 StoreItem 和 Store 类,代码逻辑更加清晰和模块化。快速启动器 不仅支持打开网址,还能打开文件夹、启动应用程序和执行系统命令。你可以根据需要在 store.txt 中添加更多映射关系,实现快速访问常用资源的目标。希望这个工具能帮助你提高工作效率!

 

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

本版积分规则

399

主题

0

回帖

1207

积分

金牌会员

积分
1207

QQ|智能设备 | 粤ICP备2024353841号-1

GMT+8, 2025-3-10 15:08 , Processed in 0.801386 second(s), 27 queries .

Powered by 智能设备

©2025

|网站地图