自制Python第三方库安装器

刚刚初学Python时,安装第三方库非常麻烦,速度又慢,即使选择国内镜像,还得经常敲pip命令来安装,总之很麻烦,pycharm上安装第三库也特别慢,于是用tkinter库写了第三库安装器,之后安装第三方库就特别方便。

代码如下

import os
import tkinter as tk
import tkinter.messagebox
from tkinter import ttk


class Library_Installer_for_Python(tk.Tk):
    def __init__(self):
        super().__init__()
        self.adjust_window(window_width=500,window_height=300)
        self.title('Library Installer for Python')
        self.Creat_Widgets()

    def adjust_window(self,window_width,window_height):
        screen_width = self.winfo_screenwidth()
        screen_height = self.winfo_screenheight()
        x = int((screen_width - window_width) / 2)
        y = int((screen_height - window_height)/2)
        self.geometry("{}x{}+{}+{}".format(window_width,window_height,x,y))

    def Creat_Widgets(self):
        # 第一个标签,用户提示信息
        self.lael1 = tk.Label(self, text="欢 迎 !", width=40, height=2, font=('微软雅黑', 16)).pack(side='top')
        # 输入库名标签
        self.input_library = tk.Label(self, text='输入库名:', width=12, height=2, font=('微软雅黑', 12), anchor='w')
        self.input_library.place(x=60, y=50)

        # 用户输入框
        self.entry_library = tk.Entry(self, show=None, font=('微软雅黑', 10), width=30)
        self.entry_library.place(x=180, y=65)
        # 选择输入源标签
        self.source = tk.Label(self, text='选择数据源:', width=12, height=2, font=('微软雅黑', 12), anchor='w')
        self.source.place(x=60, y=100)
        # 数据源下拉菜单
        self.choose_source = ttk.Combobox(self, font=('微软雅黑', 10), width=28,
                                          value = ('默认源(不建议,下载很慢)',
                                                   'https://pypi.tuna.tsinghua.edu.cn/simple',
                                                   'https://mirrors.aliyun.com/pypi/simple/',
                                                   'https://pypi.mirrors.ustc.edu.cn/simple/',
                                                   'https://pypi.hustunique.com/',
                                                   'https://pypi.sdutlinux.org/',
                                                   'https://pypi.douban.com/simple/'),state = 'readonly')
        self.choose_source.current(1)
        self.choose_source.place(x=180, y=115)
        # 按钮用来安装,触发结果显示命令
        self.intall = tk.Button(self, text='安装', width=10, height=1, font=('微软雅黑', 12), command=self.get_result)
        self.intall.place(x=180, y=165)
        # 注意事项
        self.notices = tk.Button(self, text='注意事项', width=10, height=1, font=('微软雅黑', 12), command=self.notices_command)
        self.notices.place(x=320, y=165)
        # 显示作者信息
        self.version = tk.Label(self, width=10, height=2, font=('微软雅黑', 12), text='version 2.0')
        self.version.place(x=350, y=250)

        # 获取用户输入的库名

    def get_library_list(self):
        s = self.entry_library.get()
        s = s.replace(' ', '')
        s = s.replace(',', ',')
        if s == '':
            return s
        else:
            ls = s.split(',')
            return ls

    # 判断该库是否安装
    def isinstalled(self, item):
        all = os.popen('pip list').read()
        if item in all:
            return True
        else:
            return False

    # 使用os库来安装第三方库
    def in_stall(self, item):
        if self.choose_source.get() == '默认源(不建议,下载很慢)':
            os.system("pip install " + item)
        else:
            os.system("pip install -i " + self.choose_source.get() + ' ' + item)

    def install_all(self, ls):
        result = []
        for item in ls:
            if self.isinstalled(item) == True:
                result.append('您已经安装过 ' + item)
            else:
                self.in_stall(item)
                if self.isinstalled(item) == True:
                    result.append(item + ' 安装成功')
                else:
                    result.append(item + ' 安装失败')
        return result

    def get_result(self):
        ls = self.get_library_list()
        if ls == '':
            tkinter.messagebox.showerror(title='Error', message='Error! 你没有输入任何库名!')
        else:
            # 对于用户输入的每一个逐一判断和安装
            result = self.install_all(ls)
            # 最后会出输出
            result = '\n'.join(result)
            tkinter.messagebox.showinfo(title='result', message=result)

    def notices_command(self):
        notices_Str = ['这是一款简易的Python第三方库安装器,需要注意',
                       '1.使用该安装器前需确保安装了pip库',
                       '2.输入多个库名请用","隔开',
                       '3.安装失败请查看后面黑框的提示信息',
                       '4.数据源是国内的镜像,下载会更快']
        tkinter.messagebox.showinfo(title='Notice', message='\n'.join(notices_Str))

if __name__ == '__main__':
    windows = Library_Installer_for_Python()
    windows.mainloop()

安装器界面,还是很简陋的,如果未响应,需要在后面的黑色命令提示符敲回车就能运行,前提需要安装pip库
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值