[Python] 纯文本查看 复制代码
import tkinter as tk
from tkinter import ttk, messagebox, scrolledtext
import os
import subprocess
import sys
import ctypes
class UninstallerApp:
def __init__(self, root):
self.root = root
self.root.title("百度网盘“智能看图”功能卸载工具")
self.root.geometry("650x550")
self.root.resizable(True, True)
# 设置中文字体
self.style = ttk.Style()
self.style.configure("TLabel", font=("SimHei", 10))
self.style.configure("TButton", font=("SimHei", 10))
# 创建界面组件
self.create_widgets()
# 检查是否以管理员身份运行
self.check_admin()
def create_widgets(self):
# 主框架
main_frame = ttk.Frame(self.root, padding=20)
main_frame.pack(fill=tk.BOTH, expand=True)
# 标题
title_label = ttk.Label(
main_frame,
text="百度网盘“智能看图”功能卸载工具",
font=("SimHei", 14, "bold")
)
title_label.pack(pady=(0, 20))
# 说明文本
info_text = """此工具将删除百度网盘捆绑的“智能看图”软件
请确保以管理员身份运行此程序以保证卸载效果
卸载内容包括:
1. 相关注册表项
2. 程序安装目录文件"""
info_label = ttk.Label(main_frame, text=info_text, justify=tk.LEFT)
info_label.pack(anchor=tk.W, pady=(0, 20))
# 操作按钮区域
button_frame = ttk.LabelFrame(main_frame, text="操作选择")
button_frame.pack(fill=tk.X, pady=(0, 20))
# 按钮容器
btn_container = ttk.Frame(button_frame)
btn_container.pack(pady=10, padx=10, fill=tk.X)
# 完整卸载按钮
self.uninstall_btn = ttk.Button(
btn_container,
text="开始完整卸载",
command=self.start_full_uninstall
)
self.uninstall_btn.pack(side=tk.LEFT, padx=(0, 10))
# 单独删除注册表按钮
self.delete_reg_btn = ttk.Button(
btn_container,
text="仅删除注册表项",
command=self.delete_registry_keys
)
self.delete_reg_btn.pack(side=tk.LEFT, padx=(0, 10))
# 单独删除程序文件按钮
self.delete_files_btn = ttk.Button(
btn_container,
text="仅删除程序文件",
command=self.delete_program_files
)
self.delete_files_btn.pack(side=tk.LEFT)
# 日志区域
log_frame = ttk.LabelFrame(main_frame, text="操作日志")
log_frame.pack(fill=tk.BOTH, expand=True, pady=(0, 20))
self.log_text = scrolledtext.ScrolledText(
log_frame,
wrap=tk.WORD,
font=("SimHei", 10)
)
self.log_text.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
self.log_text.config(state=tk.DISABLED)
# 退出按钮
exit_btn = ttk.Button(
main_frame,
text="退出",
command=self.root.quit
)
exit_btn.pack(side=tk.RIGHT)
def log(self, message):
"""向日志区域添加信息"""
self.log_text.config(state=tk.NORMAL)
self.log_text.insert(tk.END, message + "\n")
self.log_text.see(tk.END)
self.log_text.config(state=tk.DISABLED)
self.root.update_idletasks()
def check_admin(self):
"""检查是否以管理员身份运行"""
try:
is_admin = (os.getuid() == 0)
except AttributeError:
is_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0
if not is_admin:
self.log("警告:程序未以管理员身份运行,可能导致卸载不完整!")
self.log("建议关闭程序,右键选择“以管理员身份运行”")
else:
self.log("已确认管理员权限,可正常卸载")
def run_reg_command(self, command):
"""执行注册表命令"""
try:
result = subprocess.run(
command,
shell=True,
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
return True, result.stdout
except subprocess.CalledProcessError as e:
return False, e.stderr
def delete_registry_keys(self):
"""删除相关注册表项"""
# 禁用按钮防止重复操作
self._disable_buttons()
self.log("\n==========================================")
self.log("开始删除注册表项...")
# 注册表项列表
reg_keys = [
("HKEY_CLASSES_ROOT\\BaiduNetdiskImageViewerAssociations", "删除 [1/3]"),
("HKEY_CURRENT_USER\\Software\\Baidu\\BaiduNetdiskImageViewer", "删除 [2/3]")
]
# 删除注册表项
for key, msg in reg_keys:
self.log(f"{msg} {key}")
success, _ = self.run_reg_command(f'reg delete "{key}" /f >nul 2>&1')
if success:
self.log(" ✅ 删除成功")
else:
self.log(" ❌ 删除失败或项目不存在")
# 删除特定值
self.log("删除 [3/3] HKEY_CURRENT_USER\\Software\\RegisteredApplications 中的 BaiduNetdiskImageViewer")
success, _ = self.run_reg_command(
'reg delete "HKEY_CURRENT_USER\\Software\\RegisteredApplications" /v "BaiduNetdiskImageViewer" /f >nul 2>&1'
)
if success:
self.log(" ✅ 删除成功")
else:
self.log(" ❌ 删除失败或项目不存在")
self.log("注册表项删除操作完成")
self.log("==========================================\n")
# 恢复按钮状态
self._enable_buttons()
def delete_program_files(self):
"""删除程序文件目录"""
# 禁用按钮防止重复操作
self._disable_buttons()
self.log("\n==========================================")
self.log("开始删除程序文件...")
# 程序路径
appdata = os.getenv('APPDATA')
imageviewer_path = os.path.join(appdata, 'baidu', 'BaiduNetdisk', 'module', 'ImageViewer')
if os.path.exists(imageviewer_path):
self.log(f"找到程序目录: {imageviewer_path}")
self.log("正在删除程序文件...")
try:
# 尝试删除目录
if os.path.isdir(imageviewer_path):
# 使用rmdir命令删除目录
result = subprocess.run(
f'rmdir /s /q "{imageviewer_path}"',
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
if result.returncode == 0:
self.log(" ✅ 程序文件删除成功")
else:
self.log(" ❌ 程序文件删除失败,可能文件正在使用中")
self.log(f" 请手动删除: {imageviewer_path}")
except Exception as e:
self.log(f" ❌ 删除时发生错误: {str(e)}")
self.log(f" 请手动删除: {imageviewer_path}")
else:
self.log(" 程序目录不存在或已被删除")
self.log("程序文件删除操作完成")
self.log("==========================================\n")
# 恢复按钮状态
self._enable_buttons()
def start_full_uninstall(self):
"""开始完整卸载流程"""
# 禁用按钮防止重复操作
self._disable_buttons()
try:
self.log("==========================================")
self.log("开始完整卸载流程...")
# 先删除注册表
self.delete_registry_keys()
# 再删除程序文件
self.delete_program_files()
# 完成信息
self.log("\n==========================================")
self.log("完整卸载完成!")
self.log("==========================================")
self.log("\n建议重启计算机以确保所有更改生效")
appdata = os.getenv('APPDATA')
self.log(f"如果问题仍然存在,请检查以下路径是否还有残留文件:")
self.log(f"{appdata}\\baidu\\BaiduNetdisk\\module\\ImageViewer")
messagebox.showinfo("完成", "卸载已完成!建议重启计算机以确保所有更改生效。")
except Exception as e:
self.log(f"\n发生错误: {str(e)}")
messagebox.showerror("错误", f"卸载过程中发生错误: {str(e)}")
finally:
# 恢复按钮状态
self._enable_buttons()
def _disable_buttons(self):
"""禁用所有操作按钮"""
self.uninstall_btn.config(state=tk.DISABLED)
self.delete_reg_btn.config(state=tk.DISABLED)
self.delete_files_btn.config(state=tk.DISABLED)
def _enable_buttons(self):
"""启用所有操作按钮"""
self.uninstall_btn.config(state=tk.NORMAL)
self.delete_reg_btn.config(state=tk.NORMAL)
self.delete_files_btn.config(state=tk.NORMAL)
if __name__ == "__main__":
# 检查是否以管理员身份运行,如果不是则尝试重启
try:
is_admin = (os.getuid() == 0)
except AttributeError:
is_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0
if not is_admin:
# 尝试以管理员身份重启程序
ctypes.windll.shell32.ShellExecuteW(
None, "runas", sys.executable, " ".join(sys.argv), None, 1
)
sys.exit()
root = tk.Tk()
app = UninstallerApp(root)
root.mainloop()