有没有办法在Tkinter中使用功能区工具栏?

2024-05-13 20:27:58 发布

您现在位置:Python中文网/ 问答频道 /正文

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


Tags: 工具项目org语言httppdfwwwtk
1条回答
网友
1楼 · 发布于 2024-05-13 20:27:58

您需要为此创建一个包装器,并获得可以使用的二进制文件的版本。我构建了这个用于python3.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中内置的资源,看起来像this screenshot。复杂的一点是将一些功能区标记资源放入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。在

相关问题 更多 >