Tkinter 帮助查看器

发布于 2024-08-16 18:30:07 字数 1432 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌能量女王 2024-08-23 18:30:07

或者只是使用标准库中的 webbrowser 模块启动外部 Web 浏览器。

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

要编写文档,请查看 sphinx

Or just launch an external web browser, using the webbrowser module from the standard library.

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

For writing your documentation, have a look at sphinx.

素染倾城色 2024-08-23 18:30:07

您可以坚持用 html 编写,然后使用如下所示的内容: Tkhtml 显示 html相当不错,而且相当轻。 :)

这是Python 包装器。我希望这有帮助。

You could stick with writting in html, and then using something like this: Tkhtml which displays html pretty well and is fairly lightweight. :)

And here is the python wrapper. I hope this helps.

℡Ms空城旧梦 2024-08-23 18:30:07

我发现包 tkinterweb (https://pypi.org/project/tkinterweb/) 提供了 HtmlFrame将显示 HTML。我想使用 markdown - Python-Markdown (https://python-markdown.github.io/) 将 markdown 转换为 HTML,所以我同时使用了两者。两者都可以通过 pip 安装。

pip install markdown
pip install tkinterweb

下面是一些示例代码:

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)
temp_html = tempfile.NamedTemporaryFile(mode='w')
f = open(temp_html.name, 'w')
f.write(m_html)
f.flush()
frame.load_file(f.name)
frame.pack(fill="both", expand=True)
root.mainloop()

如果将 markdown 生成的 HTML 与 Wikipedia 条目中的 HTML 进行比较,您会发现它确实具有破解功能。然而,HtmlFrame 并非如此,但对于基本文档来说可能足够好了。

更新:我发现 tkinterweb 基于 tkhtml,因此该解决方案确实存在一些与此处发布的其他解决方案相同的缺陷。

I found that package tkinterweb (https://pypi.org/project/tkinterweb/) provides HtmlFrame that will display HTML. I wanted to used markdown - Python-Markdown (https://python-markdown.github.io/) converts markdown into HTML, so I used both. Both are pip-installable.

pip install markdown
pip install tkinterweb

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)
temp_html = tempfile.NamedTemporaryFile(mode='w')
f = open(temp_html.name, 'w')
f.write(m_html)
f.flush()
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. However, HtmlFrame not so, but probably good enough for basic documentation.

Update: I discovered that tkinterweb is based on tkhtml, so this solution does suffer some of the same deficiencies as others posted here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文