0

Have a working script that lets me select a PS script and pipes the output to a file. I'm trying to use the stdout=PIPE and instead output it to the blank tkinter text box. I'm about as green as they get with programming and I've made numerous attempts to get this working without success. Could someone point me in the right direction? I didn't put any attempts in here because I felt like they were completely off and didn't want to muddy the waters..

from tkinter import *
import tkinter.messagebox as box
import os, subprocess

window = Tk()
window.title( 'PowerShell Script' )

frame = Frame(window)

fldrPath = "\\\\myComputer\share\Power Shell\ps"

listbox = Listbox(frame)

for name in os.listdir(fldrPath):
    listbox.insert('end', name)

def selection():
    fileList = listbox.curselection()
    for file in fileList:
        subprocess.Popen(["powershell.exe", '-ExecutionPolicy', 
                              'Unrestricted', '-File', fldrPath + '\\' +
                              listbox.get(file)], stdout=file_object)

# Use this in selection function as a stdout
file_object = open('c:\\result.txt', 'w')

output = Text(window, width=75, height=6, wrap=WORD, background="white")

btn = Button(frame, text='Run Script', command=selection)

btn.pack(side=RIGHT, padx=5)
listbox.pack(side=LEFT)
frame.pack(padx=30, pady=30)
output.pack(fill=BOTH, expand=1, padx=5, pady=5)

window.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301
Nick Biolo
  • 35
  • 1
  • 5
  • There's an example of doing something sort of like what you want to do in this [answer of mine](https://stackoverflow.com/questions/48977473/display-the-output-of-the-program-on-gui-with-tkinter/49016673#49016673) to the question [Display the output of the program on GUI with tkinter?](https://stackoverflow.com/questions/48977473/display-the-output-of-the-program-on-gui-with-tkinter). The central concept being the creation of a separate Python tkinter-based process and piping output data streams to it via a `Queue`. – martineau Feb 21 '19 at 22:09
  • If you want to put the output of command into `Text` box, try `output.insert('end', subprocess.Popen(['powershell.exe', ...], stdout=subprocess.PIPE).communicate()[0])`. – acw1668 Feb 22 '19 at 01:09

0 Answers0