How to pass help content (tkinter.Tk ()) to python file

I'm trying to pipe the output help(tkinter.Tk())

to a python file, but for some reason it doesn't work.

I wanted to try this without using any module subprocess

. Below is the code.

import tkinter

window=tkinter.Tk()

with open('C:\\Users\\aryan21710\\help_output.txt','a') as f:
    #f.write(help(tkinter.Tk()))
    print (help(tkinter.Tk()),file=f)

with open('C:\\Users\\aryan21710\\help_output.txt','r') as f:
    for line in f:
        line=line.split('\n')
        if 'destroy' in line:
            print('DESTROY FOUND IN FOLLOWING LINE:- {0}'.format(line))

      

+3


source to share


1 answer


Since it help()

starts interactive Python and returns nothing, you must start it in a subprocess and read its output:

import subprocess
cmd = 'python3 -c "import tkinter; help(tkinter.Tk())"'
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)

      



You can now access cmd.stdout

to get the lines of the output help()

. Keep in mind that they are bytes, but you can easily convert these strings to one multi-line string with:

help_text = ''.join(line.decode('utf-8') for line in process.stdout)

      

+1


source







All Articles