while executing the .exe file created with Pyinstaller of my py code I got a problem, when changing to merge pdf window it dont show the tkinterdnd2 drag and drop function (Left Image is how .exe shows the window and the second one how the interpreter shows the window.
I try to check the console I saw that there were some basic modules imported, as numpy and TclTk that is for tkinter but I never saw PyPDF2 or tkinterdnd2, and my guesses were that it didn´t process the modules. But I don’t know
Pd. I use the settings –onefile –windowed file.py
import tkinter as tk
from tkinter import messagebox
from tkinterdnd2 import DND_FILES, TkinterDnD
import PyPDF2
import os
file_paths = []
def window_merge():
root.destroy()
merge_root = TkinterDnD.Tk()
merge_root.title("Drag and Drop Files")
merge_root.geometry("300x240")
merge_root.maxsize(400,350)
merge_root.config(bg="lightgrey")
listbox = tk.Listbox(merge_root, width=60, height=10)
listbox.pack(pady=5)
reset_button = tk.Button(merge_root, text="ResetList", command=lambda: listbox.delete(0, tk.END))
reset_button.pack(side=tk.RIGHT)
def drop(event):
global file_paths
file_list = event.data.split()
for file in file_list:
listbox.insert(tk.END, file)
file_paths.append(file)
merge_root.drop_target_register(DND_FILES)
merge_root.dnd_bind('<<Drop>>', drop)
back_button = tk.Button(merge_root, text="Back", command=lambda: go_back(merge_root))
back_button.pack(side=tk.LEFT, padx=20, pady=10)
pdf_merge_button = tk.Button(merge_root, text="Merge PDF's",command=merge_pdfs)
pdf_merge_button.pack(pady=10)
merge_root.mainloop()
def window_split():
root.destroy()
split_root = TkinterDnD.Tk()
split_root.title("Drag and Drop Files")
split_root.geometry("300x240")
split_root.maxsize(400,350)
split_root.config(bg="lightgrey")
listbox = tk.Listbox(split_root, width=60, height=10)
listbox.pack(pady=5)
reset_button = tk.Button(split_root, text="ResetList", command=lambda: listbox.delete(0, tk.END))
reset_button.pack(side=tk.RIGHT)
def drop(event):
global file_paths
file_list = event.data.split()
for file in file_list:
listbox.insert(tk.END, file)
file_paths.append(file)
split_root.drop_target_register(DND_FILES)
split_root.dnd_bind('<<Drop>>', drop)
back_button = tk.Button(split_root, text="Back", command=lambda: go_back(split_root))
back_button.pack(side=tk.LEFT, padx=20, pady=10)
pdf_merge_button = tk.Button(split_root, text="Split PDF's",command=split_pdf)
pdf_merge_button.pack(pady=10)
split_root.mainloop()
def go_back(current_root):
current_root.destroy()
window_home()
def window_home():
global root
root = tk.Tk()
root.title("MERGE & Spliter")
message_label = tk.Label(root, text="What do you want to do?")
message_label.pack(pady=20)
button1 = tk.Button(root, text="Merge", command=window_merge)
button1.pack(side=tk.LEFT, padx=20, pady=10)
button2 = tk.Button(root, text="Split", command=window_split)
button2.pack(side=tk.RIGHT, padx=20, pady=10)
root.mainloop()
def merge_pdfs():
global file_paths
output_path = get_output_path(file_paths)
merger = PyPDF2.PdfMerger()
for pdf in file_paths:
merger.append(pdf)
with open(output_path,'wb')as output_file:
merger.write(output_file)
messagebox.showinfo("Merge Complete", f"PDFs have been merged into {output_path}")
def get_output_path(pdf_files):
first_value = pdf_files[0]
last_backslash_index = first_value.rfind('/')
output_file_path = first_value[:last_backslash_index]
values = first_value.split('/')
value = values[-2]
output_file_path = output_file_path+'/'+value+'.pdf'
return output_file_path
def get_output_dir(pdf_files):
first_value = pdf_files[0]
last_backslash_index = first_value.rfind('/')
output_file_path = first_value[:last_backslash_index]
print(output_file_path)
return output_file_path
def split_pdf():
global file_paths
for file_path in file_paths:
with open(file_path,'rb') as input_file:
reader = PyPDF2.PdfReader(input_file)
base_name = os.path.splitext(os.path.basename(file_path))[0]
for page_num in range(len(reader.pages)):
output_dir = get_output_dir(file_paths)
writer = PyPDF2.PdfWriter()
writer.add_page(reader.pages[page_num])
output_path = f'{output_dir}/{base_name}_page_{page_num + 1}.pdf'
with open(output_path,'wb') as output_file:
writer.write(output_file)
messagebox.showinfo("Split Complete", f"PDFs have been split into {output_path}")
window_home()
Edit: While executing the file with console I saw that ir raise error _tkinter.TclError: can’t find package tkdnd