简体   繁体   English

Tkinter 帮助查看器

[英]Tkinter help viewer

I have a simple Tkinter app in Python.我在 Python 中有一个简单的 Tkinter 应用程序。 I'd like to add help document to it;我想为其添加帮助文档; what is the simplest way to integrate an help viewer to the app?将帮助查看器集成到应用程序的最简单方法是什么? Preferably cross-platform (although I primarily use Windows)?最好是跨平台的(虽然我主要使用 Windows)?

I can imagine writing the help in plain HTML.我可以想象用纯 HTML 编写帮助。

Or just launch an external web browser, using the webbrowser module from the standard library. 或者使用标准库中的webbrowser模块启动外部Web浏览器。

import webbrowser
webbrowser.open('/path/to/help/file.html')

For writing your documentation, have a look at sphinx . 要编写文档,请查看sphinx

You could stick with writting in html, and then using something like this: Tkhtml which displays html pretty well and is fairly lightweight. 您可以坚持使用html中的写入 ,然后使用这样的东西: Tkhtml显示html相当好,并且相当轻量级。 :) :)

And here is the python wrapper . 这是python包装器 I hope this helps. 我希望这有帮助。

I found that package tkinterweb ( https://pypi.org/project/tkinterweb/ ) provides HtmlFrame that will display HTML.我发现包 tkinterweb ( https://pypi.org/project/tkinterweb/ ) 提供了将显示 HTML 的 HtmlFrame。 I wanted to used markdown - Python-Markdown ( https://python-markdown.github.io/ ) converts markdown into HTML, so I used both.我想使用 markdown - Python-Markdown ( https://python-markdown.github.io/ ) 将 markdown 转换为 HTML,所以我同时使用了两者。 Both are pip-installable.两者都是 pip 可安装的。 Here's some sample code:这是一些示例代码:

import tkinter as tk
from tkinterweb import HtmlFrame
import markdown
import tempfile

root = tk.Tk()
frame = HtmlFrame(root, messages_enabled=False)

m_text = (
    'Markdown sample (https://en.wikipedia.org/wiki/Markdown#Examples)\n'
    '\n'
    'Heading\n'
    '=======\n'
    '\n'
    'Sub-heading\n'
    '-----------\n'
    '\n'
    '# Alternative heading #\n'
    '\n'
    'Paragraphs are separated\n'
    'by a blank line.\n'
    '\n'
    'Two spaces at the end of a line  \n'
    'produce a line break.\n'
    '\n'
    'Text attributes _italic_, **bold**, `monospace`.\n'
    '\n'
    'Horizontal rule:\n'
    '\n'
    '---\n'
    '\n'
    'Bullet lists nested within numbered list:\n'
    '\n'
    '  1. fruits\n'
    '     * apple\n'
    '     * banana\n'
    '  2. vegetables\n'
    '     - carrot\n'
    '     - broccoli\n'
    '\n'
    'A [link](http://example.com).\n'
    '\n'
    '![Image](Icon-pictures.png "icon")\n'
    '\n'
    '> Markdown uses email-style\n'
    'characters for blockquoting.\n'
    '>\n'
    '> Multiple paragraphs need to be prepended individually.\n'
    '\n'
    'Most inline <abbr title="Hypertext Markup Language">HTML</abbr> is supported.\n'
)

'''
# normally read the text from a file
with open('sample.md', 'r') as f:
    m_text = f.read()
'''
m_html = markdown.markdown(m_text)
#print(m_html)
temp_html = tempfile.NamedTemporaryFile(mode='w')
with open(temp_html.name, 'w') as f:
    f.write(m_html)
frame.load_file(f.name)
frame.pack(fill="both", expand=True)
root.mainloop()

If you compare the generated HTML from markdown with that in the Wikipedia entry you can see it does a cracking job.如果将markdown生成的 HTML 与 Wikipedia 条目中的 HTML 进行比较,您会发现它做得很好。 However, HtmlFrame isn't perfect, but good enough for basic markdown.然而, HtmlFrame并不完美,但对于基本的HtmlFrame来说已经足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM