Is there a way to use ribbon toolbars in Tkinter?

后端 未结 1 772
野性不改
野性不改 2020-12-31 19:42

I am yet to decide what language and tools to use for my next project. I would love to use python, but I would like to implement ribbon toolbars. Some work has been done in

相关标签:
1条回答
  • 2020-12-31 20:06

    You need to create a wrapper for this and get a version of the binary you can use. I built this for use with Python 3.4 and copied it to tkribbon1.0-x86_64.zip. You should unzip this in the Python/tcl subdirectory so the version of tcl used by python can load it.

    A minimal wrapper looks like this:

    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()
    

    Running this uses the resources built-in to the tkribbon dll and looks like this screenshot. The complicated bit is going to be getting some Ribbon markup resources into a DLL for loading.

    You can use this example to load ribbons from existing applications. For instance, python Ribbon.py c:\Windows\System32\mspaint.exe MSPAINT_RIBBON will load up the ribbon resource from mspaint. The resource name in this case has to be included as the default is APPLICATION_RIBBON. For your own ribbon, using uicc to build a .rc file, then rc /r file.rc to produce a .res file and finally link -dll -out:file.dll file.rc -noentry -machine:AMD64 seems to work to produce a resource only DLL that works with this extension.

    0 讨论(0)
提交回复
热议问题