1+ # Author: primetime43
2+ # GitHub: https://github.com/primetime43
3+
4+ import tkinter as tk
5+ from tkinter import ttk
6+ import subprocess
7+ import os
8+ import threading
9+
10+ def run_ps1 (uninstall = False ):
11+ # This function handles the installation and uninstallation of pyenv
12+
13+ # Skip the check if pyenv is installed when uninstalling
14+ if not uninstall :
15+ # Check if pyenv is installed by running a PowerShell command
16+ try :
17+ version = subprocess .check_output (['powershell' , '-Command' , 'pyenv --version' ])
18+ output_text .insert (tk .END , "pyenv is already installed.\n " )
19+ output_text .insert (tk .END , version .decode () + "\n " )
20+ output_text .see (tk .END )
21+ return # Return immediately if pyenv is already installed
22+ except subprocess .CalledProcessError :
23+ pass # If pyenv is not installed, continue with the installation
24+
25+ # If pyenv is not installed and uninstall is requested, display message
26+ if uninstall :
27+ try :
28+ subprocess .check_output (['powershell' , '-Command' , 'pyenv --version' ])
29+ except subprocess .CalledProcessError :
30+ output_text .insert (tk .END , "pyenv is not installed, so it cannot be uninstalled.\n " )
31+ output_text .see (tk .END )
32+ return
33+
34+ # Check if the installation script is present, if not, download it
35+ if not os .path .exists ("./install-pyenv-win.ps1" ):
36+ ps_command = 'Invoke-WebRequest -UseBasicParsing -Uri "https://raw.githubusercontent.com/pyenv-win/pyenv-win/master/pyenv-win/install-pyenv-win.ps1" -OutFile "./install-pyenv-win.ps1"'
37+ command = ['powershell' , '-Command' , ps_command ]
38+ subprocess .run (command , stdout = subprocess .PIPE , stderr = subprocess .STDOUT )
39+
40+ # Prepare and execute the installation or uninstallation command
41+ if uninstall :
42+ output_text .insert (tk .END , "Starting uninstallation...\n " )
43+ command = ['powershell' , '-Command' , '&"./install-pyenv-win.ps1" -Uninstall' ]
44+ else :
45+ output_text .insert (tk .END , "Starting installation...\n " )
46+ command = ['powershell' , '-Command' , '&"./install-pyenv-win.ps1"' ]
47+
48+ output_text .see (tk .END )
49+
50+ # Run the command in a subprocess and capture the output
51+ process = subprocess .Popen (command , stdout = subprocess .PIPE , stderr = subprocess .STDOUT )
52+
53+ # Read and display the output from the subprocess
54+ while True :
55+ output = process .stdout .readline ()
56+ if output == '' and process .poll () is not None :
57+ break
58+ if output :
59+ output_text .insert (tk .END , output .decode ())
60+ output_text .see (tk .END )
61+ rc = process .poll ()
62+
63+ def install_update ():
64+ # Start a new thread for installing or updating pyenv
65+ threading .Thread (target = run_ps1 , args = (False ,)).start ()
66+
67+ def uninstall ():
68+ # Start a new thread for uninstalling pyenv
69+ threading .Thread (target = run_ps1 , args = (True ,)).start ()
70+
71+ def clear_output ():
72+ # Clear the output text area
73+ output_text .delete ('1.0' , tk .END )
74+
75+ def run_command ():
76+ # Run a pyenv command using PowerShell and display the output
77+ command = ['powershell' , '-Command' , f'pyenv { command_var .get ()} { params_entry .get ()} ' ]
78+ result = subprocess .run (command , check = True , stdout = subprocess .PIPE , stderr = subprocess .STDOUT )
79+ output_text .insert (tk .END , result .stdout .decode ())
80+ output_text .see (tk .END )
81+
82+ # Create the main window
83+ root = tk .Tk ()
84+ root .title ("pyenv-win GUI" ) # Set the title of the window
85+
86+ # Create the Install/Update button with left padding
87+ install_button = tk .Button (root , text = "Install/Update pyenv-win" , command = install_update )
88+ install_button .grid (row = 0 , column = 0 , sticky = 'w' , pady = (10 , 5 ), padx = (10 , 0 )) # Add 10 pixels of padding to the left
89+
90+ # Create the Uninstall button with left padding
91+ uninstall_button = tk .Button (root , text = "Uninstall pyenv-win" , command = uninstall )
92+ uninstall_button .grid (row = 1 , column = 0 , sticky = 'w' , pady = 5 , padx = (10 , 0 )) # Add 10 pixels of padding to the left
93+
94+ # List of commands for the dropdown menu
95+ commands = ['commands' , 'duplicate' , 'local' , 'global' , 'shell' , 'install' , 'uninstall' , 'update' , 'rehash' , 'vname' , 'version' , 'version-name' , 'versions' , 'exec' , 'which' , 'whence' ]
96+
97+ # Create a variable for the selected command
98+ command_var = tk .StringVar (root )
99+ command_var .set (commands [0 ]) # Set the default option
100+
101+ # Create the dropdown menu for the commands
102+ command_menu = ttk .Combobox (root , textvariable = command_var , values = commands )
103+ command_menu .grid (row = 2 , column = 0 , pady = 5 ) # Place the dropdown menu in the grid
104+
105+ # Create a frame for the parameters input
106+ params_frame = tk .Frame (root )
107+ params_frame .grid (row = 3 , column = 0 ) # Place the frame in the grid
108+
109+ # Create a label for the parameters input
110+ params_label = tk .Label (params_frame , text = "Parameters:" )
111+ params_label .pack (side = tk .LEFT ) # Place the label in the frame
112+
113+ # Create the parameters input box
114+ params_entry = tk .Entry (params_frame )
115+ params_entry .pack (side = tk .LEFT ) # Place the input box in the frame
116+
117+ # Create the Run Command button
118+ run_button = tk .Button (root , text = "Run Command" , command = run_command )
119+ run_button .grid (row = 4 , column = 0 , pady = 5 ) # Place the button in the grid
120+
121+ # Create the output text box
122+ output_text = tk .Text (root )
123+ output_text .grid (row = 5 , column = 0 , sticky = 'nsew' , pady = 5 ) # Place the text box in the grid
124+
125+ # Create the scrollbar for the output text box
126+ scrollbar = tk .Scrollbar (root , command = output_text .yview )
127+ scrollbar .grid (row = 5 , column = 1 , sticky = 'ns' ) # Place the scrollbar in the grid
128+
129+ # Link the scrollbar to the output text box
130+ output_text ['yscrollcommand' ] = scrollbar .set
131+
132+ # Create the Clear Output button
133+ clear_button = tk .Button (root , text = "Clear Output" , command = clear_output )
134+ clear_button .grid (row = 6 , column = 0 , pady = (5 , 10 )) # Place the button in the grid
135+
136+ # Start the main event loop
137+ root .mainloop ()
0 commit comments