How to Install and Use TkDnD with Python Tkinter on OSX?

后端 未结 4 1178
情话喂你
情话喂你 2020-12-03 16:35

I\'ve spent some time to search for workable solution to do Drag and Drop behavior with Python Tkinter on OSX platform, and the most possible solution found is TkDnD library

4条回答
  •  情歌与酒
    2020-12-03 17:09

    This is an update from 2017, with a bit more detail, since Mac decided to make it impossible to write files to /System/Library. The following solution is also cross-platform, since I am currently writing an app for both Windows/Mac that uses TkDnD.

    The following solution works with pyInstaller, and also with Mac OS 10.12 and Windows 7.

    First, we need to get a path to where tkDnD is. By default, I place tkdnd2.8 folder next to main.py.

    import sys, os
    if getattr(sys, 'frozen', False):
        # If the application is run as a bundle, the pyInstaller bootloader
        # extends the sys module by a flag frozen=True and sets the app 
        # path into variable _MEIPASS'.
        application_path = sys._MEIPASS
    else:
        application_path = os.path.dirname(os.path.abspath(__file__))
    
    TK_DND_PATH = os.path.join(application_path,'tkdnd2.8')
    

    Note that Ellis's solution works Tcl directly, modifying the path. Make sure that SOMEWHERE, you have something along this gist:

    import tkinter as tk
    root = tk.Tk()
    root.eval('lappend auto_path {' + TK_DND_PATH + '}')
    

    After this, whenever you happen to actually import tkDnD, it will find it. I used DnD.py. Without the 'lappend auto_path' command, my program could never find tkDnD.

    https://mail.python.org/pipermail/tkinter-discuss/2005-July/000476.html

提交回复
热议问题