我正在尝试将 help(tkinter.Tk())
的输出传输到 python 文件,但由于某种原因它没有发生。
我想在不使用任何 subprocess
模块的情况下尝试这个。以下是代码。
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))
最佳答案
由于 help()
启动交互式 Python 并且不返回任何内容,因此您必须在子进程中运行它并读取其输出:
import subprocess
cmd = 'python3 -c "import tkinter; help(tkinter.Tk())"'
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
现在您可以访问 cmd.stdout
以获取 help()
输出的行。请注意它们是字节,但您可以使用以下方法轻松地将这些行转换为单个多行字符串:
help_text = ''.join(line.decode('utf-8') for line in process.stdout)
关于python - 如何将 help(tkinter.Tk()) 的内容传输到 python 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43731830/