您需要为此创建一个包装器并获取您可以使用的二进制版本。我构建了这个用于 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。