I have a simple Tkinter app in Python. 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)?
I can imagine writing the help in plain HTML.
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.
webbrowser says (re. the open function): Note that on some platforms, trying to open a filename using this function, may work and start the operating system’s associated program. However, this is neither supported nor portable.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.
tkinterhtml is no longer supported.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'
'\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.