【发布时间】:2021-03-01 22:37:05
【问题描述】:
为用户构建一个 GUI 以选择他们想要运行的 Python 脚本。每个脚本都有自己的文档字符串来解释脚本的输入和输出。我想在他们突出显示脚本后在 UI 中显示该信息,但未选择运行它,而且我似乎无法从基本程序访问文档字符串。
例如
test.py
"""this is a docstring"""
print('hello world')
program.py
index 在此示例中为 test.py,但通常不知道,因为它是用户在 GUI 中选择的任何内容。
# index is test.py
def on_selected(self, index):
script_path = self.tree_view_model.filePath(index)
fparse = ast.parse(''.join(open(script_path)))
self.textBrowser_description.setPlainText(ast.get_docstring(fparse))
【问题讨论】:
-
您已经在此处使用
ast.get_docstring的代码似乎是解决此问题的正确方法。有什么问题? -
这个here已经有答案了
-
@Comsavvy 您引用的问题与此问题无关。
-
确实如此! @e4862 正在寻找一种方法来获取
test.py的文档字符串。 -
不清楚你为什么用
ast.parse(''.join(open(script_path)))而不是ast.parse(open(script_path).read()),
标签: python python-3.8 docstring python-ast