5

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.

Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187

3 Answers3

3

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.

codeape
  • 97,830
  • 24
  • 159
  • 188
  • 1
    I didn't ask this in the original question, but one advantage of using a native help viewer is the **search** functionality. I believe sphinx has a json-based search; and that should probably meet my modest needs. Thanks – Sridhar Ratnakumar Jan 05 '10 at 02:34
  • 1
    Be aware that the doc for `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._ – ceperman Dec 31 '21 at 08:38
2

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.

Fabzter
  • 505
  • 5
  • 16
  • 1
    Here is a newer, pip-installable Python wrapper: https://bitbucket.org/aivarannamaa/tkinterhtml. It comes with TkHtml3 binaries for Windows, Mac and Linux. – Aivar Dec 16 '15 at 17:41
  • `tkinterhtml` is no longer supported. – ceperman Dec 31 '21 at 18:13
1

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.

ceperman
  • 405
  • 2
  • 8
  • What deficiencies are there exactly? – Andereoo Feb 14 '22 at 14:21
  • At least two I've found: is rendered in normal type, not monospace, and a 2nd level
      is rendered as
      .
    – ceperman Feb 17 '22 at 16:03
  • Try re-installing TkinterWeb. I can't seem to reproduce your issue with ``, but I just fixed the issues with `
      `. Thanks for letting me know about this, and if you notice any other deficiencies please feel free to report them on the [Github](https://github.com/Andereoo/TkinterWeb) page!
    – Andereoo Feb 28 '22 at 14:30