【问题标题】:Is there a way to use ribbon toolbars in Tkinter?有没有办法在 Tkinter 中使用功能区工具栏?
【发布时间】:2019-01-18 16:02:07
【问题描述】:

我还没有决定在我的下一个项目中使用什么语言和工具。我很想使用 python,但我想实现功能区工具栏。一些工作已经在 Tk (http://www.ellogon.org/petasis/bibliography/Tcl2010/TkRibbon.pdf) 中完成,但看起来它还没有在 tkinter 中实现。我能做些什么来让它工作吗?

【问题讨论】:

  • 我猜你需要围绕tkribbon 编写自己的包装库,因为显然还没有人这样做。或者写一些代码来模拟那种工具栏...

标签: python tkinter


【解决方案1】:

您需要为此创建一个包装器并获取您可以使用的二进制版本。我构建了这个用于 Python 3.4 并将其复制到 tkribbon1.0-x86_64.zip。你应该把它解压到 Python/tcl 子目录,这样 python 使用的 tcl 版本就可以加载它。

一个最小的包装器如下所示:

from tkinter import Widget
from os import path

class Ribbon(Widget):
    def __init__(self, master, kw=None):
        self.version = master.tk.call('package','require','tkribbon')
        self.library = master.tk.eval('set ::tkribbon::library')
        Widget.__init__(self, master, 'tkribbon::ribbon', kw=kw)

    def load_resource(self, resource_file, resource_name='APPLICATION_RIBBON'):
        """Load the ribbon definition from resources.

        Ribbon markup is compiled using the uicc compiler and the resource included
        in a dll. Load from the provided file."""
        self.tk.call(self._w, 'load_resources', resource_file)
        self.tk.call(self._w, 'load_ui', resource_file, resource_name)

if __name__ == '__main__':
    import sys
    from tkinter import *
    def main():
        root = Tk()
        r = Ribbon(root)
        name = 'APPLICATION_RIBBON'
        if len(sys.argv) > 1:
            resource = sys.argv[1]
            if len(sys.argv) > 2:
                name = sys.argv[2]
        else:
            resource = path.join(r.library, 'libtkribbon1.0.dll')
        r.load_resource(resource, name)
        t = Text(root)
        r.grid(sticky=(N,E,S,W))
        t.grid(sticky=(N,E,S,W))
        root.grid_columnconfigure(0, weight=1)
        root.grid_rowconfigure(1, weight=1)
        root.mainloop()
    main()

运行它使用 tkribbon dll 内置的资源,看起来像 。复杂的一点是将一些 Ribbon 标记资源放入 DLL 中进行加载。

您可以使用此示例从现有应用程序加载功能区。例如,python Ribbon.py c:\Windows\System32\mspaint.exe MSPAINT_RIBBON 将从 mspaint 加载功能区资源。在这种情况下,资源名称必须包含在内,默认为 APPLICATION_RIBBON。对于您自己的功能区,使用 uicc 构建 .rc 文件,然后使用 rc /r file.rc 生成 .res 文件,最后 link -dll -out:file.dll file.rc -noentry -machine:AMD64 似乎可以生成与此扩展一起使用的仅资源 DLL。

【讨论】:

  • 我已经试过了,它确实工作得相当合理,所以过了一段时间,我会接受它。不过,也许有一天有人会写一个包装器。
猜你喜欢
  • 2014-05-19
  • 2015-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-23
  • 2023-04-11
  • 1970-01-01
相关资源
最近更新 更多