I having a problem with “get_current_directory” in here: INDEX-it-4.0**.exe** .
As .exe it shows current_directory as: C:\Users\vmars\AppData\Local\Temp_MEI224762
When I run it as INDEX-it-4.0**.py** , all is well :
current_directory shows as : ‘C:\Index-it-py’ , as it should .
Pls , how to fix ?
in this code:
def start_program(self):
self.file_content = []
filename = filedialog.askopenfilename(initialdir=self.get_current_directory(), title="Select a file to open")
if filename:
with open(filename, 'r') as file:
self.input_text.delete('1.0', tk.END)
self.middle_listbox.delete(0, tk.END) # Clear the listbox before populating
line_number = 1
added_words = set()
for line in file:
line_content = line.strip()
self.file_content.append(line_content)
self.input_text.insert(tk.END, f"{line_number} {line_content}\n")
# Populate the middle_listbox with unique words
words = line_content.split()
for word in words:
if word not in added_words:
self.middle_listbox.insert(tk.END, word)
added_words.add(word)
line_number += 1
self.update_line_count()
I am wondering would I be better off using QT instead of TK .
The correct method should be in the docs for whatever tool has packaged the Python code into an .exe.
If pathlib.Path.cwd() doesn’t work in this non-standard environment, if a Python run-time is bundled in the .exe, maybe sys.executable is a better reference than __file__, (to put into pathlib.Path(...).parent). Or you could try subprocess.run(‘cd’) (or ‘pwd’ on Posix).
pathlib.Path.cwd() will always work in that it will always obtain the current working directory. The problem is that the current working directory (i.e. wherever you opened your terminal, or wherever the executable is located if launched by the desktop on Windows, or usually the home directory on other platforms) is rarely what anyone wants.
The MEI directory isn’t ever the default working directory so I doubt this current_directory() features cwd().
echo
set PATH=%PATH%;C:\Users\vmars\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\Local-packages\Python312\Scripts
pyinstaller --onefile --windowed C:\Index-it-py\~~~INDEX-it-TK-MINI.py
pause
INDEX-it-TK-MINI.py
import tkinter as tk
from tkinter import filedialog, messagebox
import os
class IndexItApp:
def __init__(self, master):
self.master = master
master.title("Index-It-TK-MINI")
master.resizable(True, True)
# Address entry for displaying the current working directory
self.address_entry = tk.Entry(master, width=70)
self.address_entry.insert(0, self.get_current_directory())
self.address_entry.grid(row=0, column=1, padx=10, pady=10, sticky="ew")
# Start button
self.start_button = tk.Button(master, text="START", command=self.start_program)
self.start_button.grid(row=1, column=0, padx=10, pady=10)
def get_current_directory(self):
try:
return os.getcwd() # Default to current working directory
except Exception as e:
messagebox.showerror("Error", f"Failed to get the current directory: {e}")
return ""
def start_program(self):
# Opens a file dialog to select a file
filename = filedialog.askopenfilename(initialdir=self.get_current_directory(), title="Select a file to open")
if filename:
try:
with open(filename, 'r') as file:
content = file.read()
messagebox.showinfo("File Content", f"File '{os.path.basename(filename)}' opened successfully!")
except Exception as e:
messagebox.showerror("Error", f"Failed to open file: {e}")
if __name__ == "__main__":
root = tk.Tk()
app = IndexItApp(root)
root.mainloop()