[Python] pyenv管理アプリ v0.0.5 pip関連操作

以下の4機能を追加しました。
1.pipによりインストールしたライブラリのリスト表示
2.アップデート可能なライブラリのリスト表示
3.ライブラリのアップデート
4.新規ライブラリのインストール

これでひと段落したのでexe化して普段使いします。

import subprocess,datetime,os
import tkinter as tk
import tkinter.font as font
from tkinter import ttk

# 16進数カラーコード
# #EDFFBE #C2EEFF #7B3CFF

class FrameA(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.configure(background = '#EDFFBE')
        self.grid(row=1,column=1, sticky=tk.NSEW, padx=5, pady=5)

class FrameB(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.configure(background = '#C2EEFF',borderwidth=0)
        self.grid(row=1,column=0, sticky=tk.NSEW, padx=5, pady=5)
        # Text設定
        self.text = tk.Text(self,height=20,width=45,highlightbackground="#f0f8ff",background = '#C2EEFF',foreground = '#8b0000')
        # ハイライトを消す場合
        # self.text = tk.Text(self,height=20,width=45,highlightthickness=0,background = '#C2EEFF',foreground = '#8b0000')

        # Y方向スクロールバーの設定
        self.scrollable_frame = ttk.Frame(self.text)
        self.scrollbar_y = ttk.Scrollbar(self, orient="vertical", command=self.text.yview,style= 'Vertical.TScrollbar')
        self.scrollbar_y.pack(side=tk.RIGHT, fill="y")

        self.text.configure(yscrollcommand=self.scrollbar_y.set)
        self.text.pack(side=tk.LEFT, fill="both", expand=True)

def version_check():
    proc = subprocess.run("pyenv versions", shell=True, stdout= subprocess.PIPE, stderr = subprocess.PIPE)
    return proc.stdout

def version_change(num):
    print(num)
    cmd = f"pyenv global {num}"
    subprocess.run(cmd, shell=True, stdout= subprocess.PIPE, stderr = subprocess.PIPE)
    proc = subprocess.run("pyenv versions", shell=True, stdout= subprocess.PIPE, stderr = subprocess.PIPE)
    return proc.stdout

def list_text():
    # frameB全ウィジェットのinfoを取得
    children = frameB.winfo_children()
    text_list = [entry for entry in children if type(entry)==tk.Text]

    return text_list

def auto_scroll():
    list_text()[0].see("end")

def piplist_check():
    proc = subprocess.run("python -m pip list", shell=True, stdout= subprocess.PIPE, stderr = subprocess.PIPE)
    return proc.stdout

def pipupd_check():
    proc = subprocess.run("python -m pip list --outdated", shell=True, stdout= subprocess.PIPE, stderr = subprocess.PIPE)
    return proc.stdout

def lib_update(lib):
    print(lib)
    cmd = f"python -m pip install -U {lib}"
    proc = subprocess.run(cmd, shell=True, stdout= subprocess.PIPE, stderr = subprocess.PIPE)
    return proc.stdout

def lib_install(lib):
    print(lib)
    cmd = f"python -m pip install {lib}"
    proc = subprocess.run(cmd, shell=True, stdout= subprocess.PIPE, stderr = subprocess.PIPE)
    return proc.stdout

# ウィンドウ作成
root = tk.Tk()
root.title("PYENV MANAGER v0.0.5")
root.geometry("480x330")
root.configure(bg='#EDFFBE')
style = ttk.Style()
style.theme_use('classic')
style.configure("MyWidget.TButton", gripcount=0,
                background="#7B3CFF", foreground="#EEFFFF",darkcolor='#7B3CFF', lightcolor="LightGreen",
                troughcolor="gray", bordercolor="gray", arrowcolor="white")
style.configure("MyWidget2.TButton", gripcount=0,
                background="#ffe4b5", foreground="#556b2f",darkcolor='#ffe4b5', lightcolor="LightGreen",
                troughcolor="gray", bordercolor="gray", arrowcolor="white")
style.configure("MyWidget3.TButton", gripcount=0,
                background="#2f4f4f", foreground="#EEFFFF",darkcolor='#2f4f4f', lightcolor="LightGreen",
                troughcolor="gray", bordercolor="gray", arrowcolor="white")
style.configure("MyWidget4.TButton", gripcount=0,
                background="#00008b", foreground="#EEFFFF",darkcolor='#00008b', lightcolor="LightGreen",
                troughcolor="gray", bordercolor="gray", arrowcolor="white")
style.configure("Vertical.TScrollbar", gripcount=0,
                background="#98fb98", foreground="#98fb98", darkcolor="#f5f5dc", lightcolor="#98fb98",
                troughcolor="#EEFFFF", bordercolor="#EEFFFF", arrowcolor="#EEFFFF")

# フォント設定
my_font = font.Font(root,family="System",size=18,weight="normal")
my_font2 = font.Font(root,family="System",size=16,weight="normal")
my_font3 = font.Font(root,family="System",size=14,weight="normal")
my_font4 = font.Font(root,family="System",size=12,weight="normal")

# Frame設定
frame = tk.Frame(root,background = '#EDFFBE')
frame.grid(row=0,column=0, sticky=tk.NSEW, padx=5, pady=10)
frameA = FrameA(master=frame)
frameB = FrameB(master=frame)

# Entry設定
entry = ttk.Entry(frame,width=25,background = '#C2EEFF',foreground = '#8b0000',font=my_font3)
entry.insert(tk.END, 'バージョン番号 or ライブラリ名') 
entry.grid(row=0,column=0, padx=5,sticky=tk.W)

# Button設定
btn = ttk.Button(frame, text="クリア",command=lambda:entry.delete(0,tk.END),width=8,style= 'MyWidget2.TButton')
btn.grid(row=0,column=1,sticky=tk.W,padx=10)

btnA = ttk.Button(frameA, text="Python確認",command=lambda:[list_text()[0].insert(tk.END,version_check()),auto_scroll()],width=8,style= 'MyWidget.TButton')
btnA.pack(padx=5,pady=3,side=tk.TOP)

btnB = ttk.Button(frameA, text="Python変更",command=lambda:[list_text()[0].insert(tk.END,version_change(str(entry.get()))),auto_scroll()],width=8,style= 'MyWidget.TButton')
btnB.pack(padx=5,pady=3,side=tk.TOP)

btnC = ttk.Button(frameA, text="pipLIST確認",command=lambda:[list_text()[0].insert(tk.END,piplist_check()),auto_scroll()],width=8,style= 'MyWidget3.TButton')
btnC.pack(padx=5,pady=3,side=tk.TOP)

btnD = ttk.Button(frameA, text="pipUPD確認",command=lambda:[list_text()[0].insert(tk.END,pipupd_check()),auto_scroll()],width=8,style= 'MyWidget3.TButton')
btnD.pack(padx=5,pady=3,side=tk.TOP)

btnE = ttk.Button(frameA, text="pipUPDATE",command=lambda:[list_text()[0].insert(tk.END,lib_update(str(entry.get()))),auto_scroll()],width=8,style= 'MyWidget3.TButton')
btnE.pack(padx=5,pady=3,side=tk.TOP)

btnF = ttk.Button(frameA, text="pipINSTALL",command=lambda:[list_text()[0].insert(tk.END,lib_install(str(entry.get()))),auto_scroll()],width=8,style= 'MyWidget4.TButton')
btnF.pack(padx=5,pady=3,side=tk.TOP)

root.mainloop()