# =========================================================
# Execute System Commands
# =========================================================
# This script does not resize correctly
# =========================================================
from tkinter import * # Python 3
##from Tkinter import * # Python 2
import os, string, sys
# ---------------------------------------------------------
# global variables
# ---------------------------------------------------------
buttonFont = ('arial', 12)
displayFont = ('Courier', 14)
# ---------------------------------------------------------
# Tk root
# ---------------------------------------------------------
root = Tk()
root.title('Execute System Commands')
# ---------------------------------------------------------
# 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)
# ---------------------------------------------------------
#
# ---------------------------------------------------------
#--- frame to hold everything
f = Frame(root, relief='flat', padx=4, pady=4)
##f = Frame(root, relief='flat', bg='red', padx=4, pady=4)
# --- command text window
cmd = Text(f, font=displayFont, relief='groove',
borderwidth=2, width=80, height=5)
##cmd = Text(f, font=displayFont, bg='light blue',
## relief='groove', width=80, height=5)
cmd.grid(row=0, column=0, sticky=N+S+E+W)
# --- buttons
##fb1 = Frame(f, relief='flat')
fb1 = Frame(f, relief='groove', borderwidth=2)
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)
# --- results text window
results = Text(f, font=displayFont, relief='groove',
borderwidth=2, width=80, height=20)
# --- quit button
##fb2 = Frame(f, relief='flat')
fb2 = Frame(f, relief='groove', borderwidth=2)
b4 = Button(fb2, text='Quit', font=buttonFont,
command=quitCallback)
# --- rregister with the geometry manager
b1.grid(row=0, column=0)
b2.grid(row=0, column=1)
b3.grid(row=0, column=2)
fb1.grid(row=1, column=0, columnspan=3)
results.grid(row=3, column=0, columnspan=3)
b4.grid(row=0,column=0)
fb2.grid(row=4, column=0, columnspan=3)
f.grid(row=0, column=0)
# -- main loop
root.mainloop()