[Python] 纯文本查看 复制代码import sys
import os
import winreg
import tkinter as tk
from tkinter import messagebox
import subprocess
import ctypes
import re
def hide_console():
"""隐藏控制台窗口"""
if sys.platform.startswith('win'):
ctypes.windll.user32.ShowWindow(ctypes.windll.kernel32.GetConsoleWindow(), 0)
def notify_environment_change():
"""通知系统环境变量已更改"""
HWND_BROADCAST = 0xFFFF
WM_SETTINGCHANGE = 0x1A
SMTO_ABORTIFHUNG = 0x0002
result = ctypes.c_long()
SendMessageTimeoutW = ctypes.windll.user32.SendMessageTimeoutW
SendMessageTimeoutW(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
"Environment", SMTO_ABORTIFHUNG, 5000, ctypes.byref(result))
def set_environment_variable(key_path, key_name, value):
"""设置环境变量"""
try:
with winreg.OpenKey(winreg.HKEY_CURRENT_USER if key_path == "user" else winreg.HKEY_LOCAL_MACHINE,
r"Environment" if key_path == "user" else r"SYSTEM\CurrentControlSet\Control\Session Manager\Environment",
0, winreg.KEY_ALL_ACCESS) as key:
winreg.SetValueEx(key, key_name, 0, winreg.REG_SZ, value)
if key_path == "user":
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
subprocess.run(f'setx {key_name} "{value}"', startupinfo=startupinfo, shell=True)
else:
notify_environment_change()
messagebox.showinfo("成功", f"环境变量 {key_name} 已成功添加,值为 {value}")
except PermissionError:
messagebox.showerror("错误", "权限不足,请以管理员身份运行此程序。")
except Exception as e:
messagebox.showerror("错误", f"添加环境变量时出错: {e}")
def add_variable():
"""添加环境变量的主函数"""
choice = var.get()
key_name = entry_key.get()
value = entry_value.get()
# 检查输入内容是否为空
if not key_name or not value:
messagebox.showerror("错误", "环境变量名和值都不能为空,请重新输入。")
return
# 检查路径是否符合规则
pattern = r'^[C-Z]:\\(.*(\\|/)?|.*\.exe)$'
if not re.match(pattern, value):
messagebox.showerror("错误", "环境变量值必须以 C: 到 Z: 盘开头,并且以目录或 .exe 文件结尾,请重新输入。")
return
if choice == 1:
set_environment_variable("user", key_name, value)
elif choice == 2:
set_environment_variable("system", key_name, value)
else:
messagebox.showerror("错误", "无效的选项,请选择用户环境变量或系统环境变量。")
def limit_length(new_text, max_length):
"""限制输入框最大长度的函数"""
return len(new_text) ", on_enter)
button_add.bind("", on_leave)
# 运行主循环
root.mainloop()
import sys
import os
import winreg
import tkinter as tk
from tkinter import messagebox
import subprocess
import ctypes
import re
def hide_console():
"""隐藏控制台窗口"""
if sys.platform.startswith('win'):
ctypes.windll.user32.ShowWindow(ctypes.windll.kernel32.GetConsoleWindow(), 0)
def notify_environment_change():
"""通知系统环境变量已更改"""
HWND_BROADCAST = 0xFFFF
WM_SETTINGCHANGE = 0x1A
SMTO_ABORTIFHUNG = 0x0002
result = ctypes.c_long()
SendMessageTimeoutW = ctypes.windll.user32.SendMessageTimeoutW
SendMessageTimeoutW(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
"Environment", SMTO_ABORTIFHUNG, 5000, ctypes.byref(result))
def set_environment_variable(key_path, key_name, value):
"""设置环境变量"""
try:
with winreg.OpenKey(winreg.HKEY_CURRENT_USER if key_path == "user" else winreg.HKEY_LOCAL_MACHINE,
r"Environment" if key_path == "user" else r"SYSTEM\CurrentControlSet\Control\Session Manager\Environment",
0, winreg.KEY_ALL_ACCESS) as key:
winreg.SetValueEx(key, key_name, 0, winreg.REG_SZ, value)
if key_path == "user":
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
subprocess.run(f'setx {key_name} "{value}"', startupinfo=startupinfo, shell=True)
else:
notify_environment_change()
messagebox.showinfo("成功", f"环境变量 {key_name} 已成功添加,值为 {value}")
except PermissionError:
messagebox.showerror("错误", "权限不足,请以管理员身份运行此程序。")
except Exception as e:
messagebox.showerror("错误", f"添加环境变量时出错: {e}")
def add_variable():
"""添加环境变量的主函数"""
choice = var.get()
key_name = entry_key.get()
value = entry_value.get()
# 检查输入内容是否为空
if not key_name or not value:
messagebox.showerror("错误", "环境变量名和值都不能为空,请重新输入。")
return
# 检查路径是否符合规则
pattern = r'^[C-Z]:\\(.*(\\|/)?|.*\.exe)$'
if not re.match(pattern, value):
messagebox.showerror("错误", "环境变量值必须以 C: 到 Z: 盘开头,并且以目录或 .exe 文件结尾,请重新输入。")
return
if choice == 1:
set_environment_variable("user", key_name, value)
elif choice == 2:
set_environment_variable("system", key_name, value)
else:
messagebox.showerror("错误", "无效的选项,请选择用户环境变量或系统环境变量。")
def limit_length(new_text, max_length):
"""限制输入框最大长度的函数"""
return len(new_text) ", on_enter)
button_add.bind("", on_leave)
# 运行主循环
root.mainloop()