execute_commands.py

# =========================================================
# Execute System Commands
# =========================================================
# This script resizes correctly
# =========================================================

from tkinter import *            # Python 3
##from Tkinter import *          # Python 2

import os, string, sys

# ---------------------------------------------------------
# global variables
# ---------------------------------------------------------

buttonFont  = ('arial',   12)
displayFont = ('Courier', 14) 

# ---------------------------------------------------------
# button callback functions
# ---------------------------------------------------------

def execCallback():
    clearResultsCallback()
    c = cmd.get('0.0',END)
    c = c.replace('\n',' ') + ' 2>&1'
    ##print('command: {}'.format(c))
    for l in os.popen(c).readlines():
        results.insert(END,l)
        results.see(END)

def quitCallback():
    sys.exit() 

def clearCommandCallback():
    cmd.delete('0.0',END)

def clearResultsCallback():
    results.delete('0.0',END)

def displayMessage(m):
    results.insert(END,m)
    results.see(END)

# ---------------------------------------------------------
# main 
# ---------------------------------------------------------

# --- Tk root

root = Tk()
root.title('Execute System Commands')

#--- a frame to hold everything

f = Frame(root, relief='flat', padx=4, pady=4)
f.grid(row=0, column=0, sticky=N+S+E+W)

Grid.columnconfigure(root, 0, weight=1)
Grid.rowconfigure(root, 0, weight=1)

# --- command text window

cmd = Text(f, font=displayFont, relief='groove',
        borderwidth=2, width=80, height=5)
cmd.grid(row=0, column=0, sticky=N+S+E+W)

cmd.grid_columnconfigure(0, weight=1)
cmd.grid_rowconfigure(0, weight=0)

# --- action buttons

fb1 = Frame(f, relief='groove', borderwidth=2)
fb1.grid(row=1, column=0)

b1 = Button(fb1, text='Clear Command', font=buttonFont,
        command=clearCommandCallback)
b2 = Button(fb1, text='Execute Command', font=buttonFont,
        command=execCallback)
b3 = Button(fb1, text = 'Clear Results', font=buttonFont,
        command=clearResultsCallback)

b1.grid(row=0, column=0)
b2.grid(row=0, column=1)
b3.grid(row=0, column=2)

f.grid_columnconfigure(0, weight=1)
f.grid_rowconfigure(1, weight=0)

# --- results text window

results = Text(f, font=displayFont, relief='groove',
        borderwidth=2, width=80, height=20)
results.grid(row=3, column=0, sticky=N+S+E+W)
f.grid_columnconfigure(0, weight=1)
f.grid_rowconfigure(3, weight=1)

# --- quit button

fb2 = Frame(f, relief='groove', borderwidth=2)
fb2.grid(row=4, column=0)

b4 = Button(fb2, text='Quit', font=buttonFont,
        command=quitCallback)

b4.grid(row=0, column=0)

f.grid_columnconfigure(0, weight=1)
f.grid_rowconfigure(4, weight=0)

# -- main loop

root.mainloop()