#!/usr/bin/python3
# coding: utf-8
#
# http://www.forum-raspberrypi.de/Thread-python-problem-mit-tkinter-subprocess-realtime-output
#
# 17.05.2016 Copyright (C) by meigrafd (meiraspi@gmail.com) published under the MIT License
#
import time
import shlex
import tkinter
from subprocess import Popen, PIPE
# selectable commands from dropdown menu
choices = [
'apt-get update',
'apt-get upgrade -y',
'apt-get install -y',
'ls -la',
'apt-get',
]
def run_command():
if running.get() == True:
print("Error: only one running command at once allowed!")
status_label.configure(text="Error: another command is active!", fg="red")
return
command = option_choice.get()
if entry.get() is not None:
command = command + " " + entry.get()
print("\nExecuting command: %s" % command)
running.set(True)
status_label.configure(text="..running..", fg="green")
returncode = execute_command(command)
if (returncode is not None) and (returncode != 0):
print("Returncode: ", end='')
print(returncode)
status_label.configure(text="Error!", fg="red")
else:
status_label.configure(text="Done: "+command, fg="green")
running.set(False)
def stop_command():
print("Command ends ...")
status_label.configure(text="Done", fg="green")
running.set(False)
def execute_command(command):
entry.delete(0, tkinter.END)
Log.delete('1.0', tkinter.END)
process = Popen(shlex.split(command), stdout=PIPE, stderr=PIPE, bufsize=1)
for output in iter(process.stdout.readline, b''):
Log.insert(tkinter.END, (output))
Log.see(tkinter.END)
Log.update()
print(output.strip())
rc = process.poll()
process.stdout.close()
process.wait()
print("\nDone")
return rc
# TKinter Main Window
window = tkinter.Tk()
window.geometry("500x500+10+10")
window.title("Command Executor")
status_label = tkinter.Label(master=window)
status_label.configure(text=" ", fg="red")
status_label.grid(row=0, column=0)
start_button = tkinter.Button(master=window, bg="#229", fg="white", text="Run", command=run_command)
stop_button = tkinter.Button(master=window, bg="#229", fg="white", text="Stop", command=stop_command)
exit_button = tkinter.Button(master=window, bg="#229", fg="white", text="X", command=window.destroy)
proc_hdl = tkinter.IntVar()
running = tkinter.BooleanVar()
running.set(False)
option_choice = tkinter.StringVar(window)
option_choice.set(choices[0])
options = tkinter.OptionMenu(window, option_choice, *choices)
options.grid(row=1, column=0)
entry = tkinter.Entry(master=window, width=20)
entry.focus()
entry.grid(row=1, column=1)
start_button.grid(row=1, column=2)
stop_button.grid(row=1, column=3)
exit_button.grid(row=0, column=4)
Log = tkinter.Text(master=window, height=31, width=68)
Log.grid(row=2, column=0, columnspan=5)
ScrollLog = tkinter.Scrollbar(window)
ScrollLog.grid(row=2, column=5, sticky=tkinter.NE + tkinter.SE)
ScrollLog.configure(command=Log.yview)
Log.configure(yscrollcommand=ScrollLog.set)
try:
window.mainloop()
except (KeyboardInterrupt, SystemExit):
print("Schliesse Programm.")