Python Forum
[Tkinter] Problems creating standalone - can't find package tkdnd
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Problems creating standalone - can't find package tkdnd
#1
I have an tkinter app that runs fine in VS-Code/Terminal, but I can't seem to get it to work as a standalone.
The error is always a variant on the theme below.

Error:
Traceback (most recent call last): File "tkinterdnd2\TkinterDnD.py", line 55, in _require _tkinter.TclError: can't find package tkdnd During handling of the above exception, another exception occurred: Traceback (most recent call last): File "BalanceBigNumbers.py", line 89, in <module> File "tkinterdnd2\TkinterDnD.py", line 287, in __init__ File "tkinterdnd2\TkinterDnD.py", line 57, in _require RuntimeError: Unable to load tkdnd library.
I've tried every suggestion that I could find online...
I've tried explicitly including tkdnd in the pyinstaller -datas tag
I've tried using a spec file to include it
I've placed copies in the dist/ folder
I've made sure that there's a copy of tkdnd under the tcl folder and not just under tkinter
I've tried adding it into the code

import sys
import os
import pandas as pd
import tkinter as tk
from tkinter import scrolledtext
from tkinterdnd2 import DND_FILES, TkinterDnD
from decimal import Decimal  # ✅ Use Decimal to prevent precision loss

# Detect if running in a compiled PyInstaller environment
if getattr(sys, 'frozen', False):
    base_path = sys._MEIPASS  # PyInstaller extracts files to _MEIPASS
else:
    base_path = os.path.dirname(os.path.abspath(__file__))

# Set the correct tkdnd path for TkinterDnD
tkdnd_path = os.path.join(base_path, "tkdnd")
...
root = TkinterDnD.Tk()
root.tk.call("lappend", "auto_path", tkdnd_path)
root.tk.call("package", "require", "tkdnd")
I'm grasping at straws and not getting any kind of traction on this.
This is actually the first time that I've needed to use a GUI in compiled code (I need to give someone a drag and drop), so it may be something monumentally stupid and obvious that I'm missing. It does work flawlessly when I launch from VS, just not packaged.

I'm on Python 3.11 in a Windows 10 Enterprise environment with tkinterdnd2-0.4.2, tk8.6, tkdnd2.9.3
Any suggestions would be greatly appreciated.

Thanks for reading and thanks, in advance, for any suggestions.
Reply
#2
There is in the docs:
Quote:pyinstaller
When using pyinstaller, you should use the hook-tkinterdnd2.py file included to collect the TkDnD binaries and build them into the project. To use it, copy it into the base directory of your project, then run pyinstaller as follows:

pyinstaller -F -w myproject/myproject.py --additional-hooks-dir=.
Gribouillis likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
what do you mean?
Reply
#4
Quote:what do you mean?
Please expand on what it is that you don't understand.
Reply
#5
Okay, here's a shorter version of the previous response, focusing on the core issue and the most likely solution:

The problem is that PyInstaller isn't correctly packaging the tkdnd library and Tcl/Tk. Even if the files are there, the Tcl interpreter in your executable can't find them.

**The Solution (using a spec file - recommended):**

1. **Create Spec File:** pyinstaller --specfile your_script_name.py

2. **Edit Spec File:**

`python
import os
import PyInstaller.config

tcl_dir = PyInstaller.config.CONF['tcltk_library']
tkdnd_dir = os.path.join(os.path.dirname(__file__), "tkdnd") # Path to tkdnd
tkdnd_files = [(os.path.join(tkdnd_dir, f), ".") for f in os.listdir(tkdnd_dir)]

a = Analysis(['your_script_name.py'], ..., datas=tkdnd_files + [
(os.path.join(tcl_dir, 'tcl8.6'), '.'),
(os.path.join(tcl_dir, 'tk8.6'), '.'),
(os.path.join(tcl_dir, 'init.tcl'), '.'),
# ... other data
], ...)

exe = EXE(a.binaries, a.zipfile, a.datas, ...,
environment={'TCL_LIBRARY': tcl_dir}, ...) # Crucial: TCL_LIBRARY
`

3. **Rebuild:** pyinstaller your_script_name.spec

**Key points:**

* **tcl_dir:** Use PyInstaller.config.CONF['tcltk_library'] for the correct Tcl path.
* **tkdnd_files:** Add tkdnd data explicitly.
* **TCL_LIBRARY:** Set this environment variable in the EXE section.

If this doesn't work, use Dependency Walker (depends.exe) to check for missing DLLs in your executable. Also, make sure your tkdnd version is compatible with your Tk version.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [TKINTER] Problems creating directories in a selected path Vulera 2 4,048 Aug-10-2021, 06:38 PM
Last Post: Vulera

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020