Skip to content

Instantly share code, notes, and snippets.

@MattSegal
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save MattSegal/a86f10cab034e4c147f3 to your computer and use it in GitHub Desktop.
Save MattSegal/a86f10cab034e4c147f3 to your computer and use it in GitHub Desktop.
"""
Python Script Manager
allows user to select a python (.py) script from directory and run it.
the script may be selected from a list and run in a command window or opened using a text editor
user can navigate folders in ROOT_DIRECTORY
TO DO:
add 'troubleshoot' to open python intepreter in a separate cmd window
integrate command window into GUI
"""
from Tkinter import *
import os
import subprocess
# ==== user specified configuration ===== #
# alter these constants to fit your desired root directory and text editor
ROOT_DIRECTORY = os.path.normpath('C:/PartyTime')
TEXT_EDITOR_PATH = os.path.normpath('C:/PROGRA~2/Vim/'+'vim74/gvim.exe')
TEXT_EDITOR_NAME = 'Vim'
#TEXT_EDITOR_PATH = os.path.normpath('C:/Windows/notepad.exe')
#TEXT_EDITOR_NAME = 'Notepad'
def main():
""" starts up GUI """
root = Tk()
root.wm_title("Python Script Manager")
app = App(root)
root.mainloop()
class App:
def __init__(self,master):
"""
initialises all GUI elements and populates with items from PartyTime folder
"""
self.master = master
self.currentScript = StringVar()
self.currentScript.set('-')
self.currentPath = StringVar()
self.path = ROOT_DIRECTORY
self.topDir = ROOT_DIRECTORY
self.currentPath.set(self.path)
frame = Frame(master)
frame.grid(row=0)
centre = Frame(frame,height = 380,width = 360)
centre.grid(row=0,column = 1)
# ===== padding ===== #
leftPad = Frame(frame,height = 100,width = 10)
leftPad.grid(row = 0,column =0)
rightPad = Frame(frame,height=100,width=10)
rightPad.grid(row=0,column=2)
botPad = Frame(frame,height=10,width = 300)
botPad.grid(row=1,columnspan=3)
# ===== controls region ===== #
control = Frame(centre)
control.grid(row=0)
Button(control,text='Run',command=self.runScript,width=10).grid(row=3,column=0)
Button(control,text=TEXT_EDITOR_NAME,command=self.openInTextEditor,width=10).grid(row=3,column=1)
Button(control,text='Open',command=self.moveUp,width=10).grid(row=3,column=2)
Button(control,text='Up',command=self.moveDown,width=10).grid(row=3,column=3)
Button(control,text='Top Dir',command=self.setTopDir,width=10).grid(row=3,column=4)
Label(control,text='Dir:').grid(row=0,column=0)
Label(control,textvariable=self.currentPath,relief = SUNKEN,width=30).grid(row=0,column=1,columnspan=3)
Button(control,text='View',command=self.openDir,width=8).grid(row=0,column=4)
Label(control,text='Script:').grid(row=1,column=0)
Label(control,textvariable=self.currentScript,relief = SUNKEN,width=30).grid(row=1,column=1,columnspan=3)
Label(control,text='Arguments:').grid(row=2,column=0)
self.argEntry = Entry(control,width=29)
self.argEntry.grid(row=2,column=1,columnspan=3)
# ===== folder contents ===== #
boxHeight = 8
browserFrame = Frame(centre)
browserFrame.grid(row=1)
folderScroll = Scrollbar(browserFrame)
scriptScroll = Scrollbar(browserFrame)
self.folderBox = Listbox(browserFrame,yscrollcommand=folderScroll.set,width=30,height =boxHeight)
self.scriptBox = Listbox(browserFrame,yscrollcommand=scriptScroll.set,width=30,height =boxHeight)
Label(browserFrame,text='Folders in current directory:').grid(row=0,column=2)
Label(browserFrame,text='Scripts in current directory:').grid(row=0,column=0)
self.folderBox.grid(row=1,column=2)
folderScroll.grid(row=1,column=3)
self.scriptBox.grid(row=1,column=0)
scriptScroll.grid(row=1,column=1)
folderScroll.config(command=self.folderBox.yview)
scriptScroll.config(command=self.scriptBox.yview)
self.updateLists()
self.updateScriptDisplay()
def updateLists(self):
""" update lists of folders and scripts in current directory """
self.folderBox.delete(0, END)
self.scriptBox.delete(0, END)
DIR = self.path
folderList = [name for name in os.listdir(DIR)if not os.path.isfile(os.path.join(DIR, name))]
scriptList = [name for name in os.listdir(DIR)if name[-3:]=='.py']
for item in folderList:
self.folderBox.insert(END, item)
for item in scriptList:
self.scriptBox.insert(END, item)
def updateScriptDisplay(self):
""" grabs and displays currently selected script in listbox """
self.currentScript.set(self.scriptBox.get(ACTIVE))
self.master.after(250, self.updateScriptDisplay)
def setTopDir(self):
""" changes current directory to ROOT_DIRECTORY """
self.path = self.topDir
self.currentPath.set(self.path)
self.updateLists()
def moveUp(self):
""" includes selected folder in current directory """
(head,tail) = os.path.split(self.path)
if self.folderBox.get(ACTIVE) != tail:
self.path = os.path.normpath(self.path+'/'+self.folderBox.get(ACTIVE))
self.currentPath.set(self.path)
self.updateLists()
def moveDown(self):
""" changes directory from current directory to parent directory, stops at ROOT_DIRECTORY"""
if self.path != self.topDir:
(head,tail) = os.path.split(self.path)
self.path = head
self.currentPath.set(self.path)
self.updateLists()
def runScript(self):
""" runs script in cmd console """
print '# ====== start script ====== #'
args = self.argEntry.get()
process = subprocess.Popen([sys.executable, self.currentScript.get(),args], cwd=self.path)
process.wait()
print '# ====== end script ====== #\n'
def openInTextEditor(self):
""" opens current script in text editor """
target = os.path.normpath(self.path+'/'+self.currentScript.get())
target = '"'+target+'"'
#vimCmd =
textEditorCmd = TEXT_EDITOR_PATH
commandString = textEditorCmd + ' ' + target
subprocess.Popen(commandString)
def openDir(self):
""" opens current directory in windows explorer"""
commandString = 'explorer '+self.path
subprocess.Popen(commandString)
# ===== run main ===== #
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment