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.
发布评论
评论(3)
或者只是使用标准库中的 webbrowser 模块启动外部 Web 浏览器。
要编写文档,请查看 sphinx。
Or just launch an external web browser, using the webbrowser module from the standard library.
For writing your documentation, have a look at sphinx.
您可以坚持用 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.
我发现包 tkinterweb (https://pypi.org/project/tkinterweb/) 提供了 HtmlFrame将显示 HTML。我想使用 markdown - Python-Markdown (https://python-markdown.github.io/) 将 markdown 转换为 HTML,所以我同时使用了两者。两者都可以通过 pip 安装。
下面是一些示例代码:
如果将
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.
Here's some sample code:
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.