文章目录
使用场景:
经常使用Pycharm中过程中要安装或卸载一些包,尤其在安装过程中经常会遇到网络问题下载失败,然后在终端输入第三方镜像源来下载。有没有一种方式,不需要每次输镜像源地址又有快速下载第三方库包呢,而且有界面可以进行操作。
设计思路:
1、输入库名,安装和卸载第三方库包
2、安装时指定第三方库镜像源,再也不用手动每次输入镜像源
# -*- coding: utf-8 -*-
# @Author : 無涯
import tkinter as tk
from tkinter import messagebox
import subprocess
import sys
import os
def center_window(root, width, height):
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width // 2) - (width // 2)
y = (screen_height // 2) - (height // 2)
root.geometry(f'{width}x{height}+{x}+{y}')
def install_package():
package_name = entry.get().strip()
if not package_name:
messagebox.showerror("错误", "请输入要安装的库名!")
return
try:
subprocess.check_call([sys.executable, "-m", "pip", "install",
"-i", "https://pypi.tuna.tsinghua.edu.cn/simple", package_name])
messagebox.showinfo("成功", f"{package_name} 已成功安装")
except subprocess.CalledProcessError as e:
messagebox.showerror("错误", f"安装 {package_name} 出错: {e}")
def uninstall_package():
package_name = entry.get().strip()
if not package_name:
messagebox.showerror("错误", "请输入要卸载的库名!")
return
try:
subprocess.check_call([sys.executable, "-m", "pip", "uninstall", "-y", package_name])
messagebox.showinfo("成功", f"{package_name} 已成功卸载")
except subprocess.CalledProcessError as e:
messagebox.showerror("错误", f"卸载 {package_name} 出错: {e}")
# 创建主窗口
root = tk.Tk()
root.title("Python库管理器")
# 设置窗口大小和居中显示
center_window(root, 400, 200)
# 创建输入框和按钮
label = tk.Label(root, text="请输入库名:")
label.pack(pady=(20, 5))
entry = tk.Entry(root, width=40)
entry.pack(pady=10)
install_button = tk.Button(root, text="安装库", command=install_package)
install_button.pack(pady=5)
uninstall_button = tk.Button(root, text="卸载库", command=uninstall_package)
uninstall_button.pack(pady=5)
# 运行主循环
root.mainloop()
卸载:

安装:

如果觉的还不错,点个爱心吧~或者在评论区说出你的需求
1万+

被折叠的 条评论
为什么被折叠?



