【问题标题】:Tkinter help viewerTkinter 帮助查看器
【发布时间】:2010-12-31 17:28:49
【问题描述】:

我有一个简单的 Python Tkinter 应用程序。我想向它添加帮助文档;将帮助查看器集成到应用程序的最简单方法是什么?最好是跨平台的(虽然我主要使用 Windows)?

我可以想象用纯 HTML 编写帮助。

【问题讨论】:

    标签: python tkinter viewer


    【解决方案1】:

    您可以坚持用 html 编写,然后使用类似这样的东西:Tkhtml 可以很好地显示 html 并且相当轻量级。 :)

    这里是the python wrapper。我希望这会有所帮助。

    【讨论】:

    • 这是一个更新的、可通过 pip 安装的 Python 包装器:bitbucket.org/aivarannamaa/tkinterhtml。它带有适用于 Windows、Mac 和 Linux 的 TkHtml3 二进制文件。
    • tkinterhtml 不再受支持。
    【解决方案2】:

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

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

    要编写文档,请查看sphinx

    【讨论】:

    • 我在最初的问题中没有问这个问题,但使用本机帮助查看器的一个优点是 search 功能。我相信 sphinx 有一个基于 json 的搜索;这应该可以满足我的适度需求。谢谢
    • 请注意webbrowser 的文档说(关于open 函数):请注意,在某些平台上,尝试使用此函数打开文件名可能会起作用并开始操作系统的相关程序。但是,这既不支持也不可移植。
    【解决方案3】:

    我发现包 tkinterweb (https://pypi.org/project/tkinterweb/) 提供了可以显示 HTML 的 HtmlFrame。我想使用 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 的,所以这个解决方案确实存在一些与此处发布的其他解决方案相同的缺陷。

    【讨论】:

    • 究竟有哪些不足?
    • 我发现至少有两个: 以普通类型呈现,而不是等宽,第二级
        被呈现为
    猜你喜欢
    • 1970-01-01
    • 2011-03-26
    • 2011-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多