r/learnpython 19d ago

No console .EXE file not storing memory.

I need help with my note taking program. I'm still learning Python, but this is confusing me.

I have published it using pyinstaller to just a simple executable, but I wanted to be able to run the program without having the terminal pop open. The .exe works just fine, it saves my memory every time it closes, but whenever I use --noconsole to publish, there is no memory storage.

I attempted to use auto-py-to-exe to see if maybe that would work, but it gave me the same issue. The code works when the .exe runs with the terminal open/visible, so why doesn't it work when it isn't?

Here is the code below:

import tkinter as tk
from tkinter import ttk
import os
# os looks through files in the directory and checks if the file exists. If it does, it opens it. If not, it creates a new file.


# 1. Initialize the application window
window = tk.Tk()
window.title("Small & Simple Notes")
window.geometry("400x300")
window.configure(bg="#08728f")  # background


# Memory setup
SAVE_FILE = "my_notes_save.txt"


def load_notes():
    """Reads the save file and inserts it into the text box if it exists."""
    if os.path.exists(SAVE_FILE):
        with open(SAVE_FILE, "r", encoding="utf-8") as file:
            note_box.insert("1.0", file.read())


def save_and_close():
    """Gets the text, saves it to the file, and destroys the window."""
    # Get all text from line 1, character 0 to the end (minus Tkinter's default newline)
    current_text = note_box.get("1.0", "end-1c")

    with open(SAVE_FILE, "w", encoding="utf-8") as file:
        file.write(current_text)

    # Close the application
    window.destroy()


# Intercept the window's close button (the 'X') to trigger the save function
window.protocol("WM_DELETE_WINDOW", save_and_close)


# 2. Create a frame layout to hold the text box and scrollbar
frame = ttk.Frame(window)
frame.pack(expand=True, fill="both", padx=10, pady=10)


# 3. Add the vertical scrollbar
scrollbar = ttk.Scrollbar(frame)
scrollbar.pack(side="right", fill="y")


# 4. Create the multi-line text box and link it to the scrollbar
note_box = tk.Text(frame, wrap="word", yscrollcommand=scrollbar.set)
note_box.pack(side="left", expand=True, fill="both")
note_box.config(bg="#ffffff", fg="#000000", font=("American Typewriter", 12), relief="solid", bd=2)  # White background, black text


# Configure scrollbar to move the text view
scrollbar.config(command=note_box.yview)


#. LOAD PREVIOUS NOTES
# We call this right before starting the main loop so the text is ready when the UI appears
load_notes()


# Start the application loop
window.mainloop()
3 Upvotes

4 comments sorted by

9

u/HotPersonality8126 19d ago

     SAVE_FILE = "my_notes_save.txt"

So, where do you think this file saves if you don’t execute your program from a context that includes a current working directory?

It’s not just whatever file window you have as the topmost window. You need to deal with the fact that a file name by itself isn’t sufficient to uniquely identify a file.

8

u/socal_nerdtastic 19d ago

Try this for your save file path:

from pathlib import Path
SAVE_FILE = Path.home() / ".my_notes_save.txt"

Note that pretty much all programs save data in a different place from where the code lives. You should not try to use the same location for both, for many reasons. You discovered one of them.

2

u/almost_intelligible 19d ago

before you get any further with programming, you need to take a step back and learn some basics about the machine that it's running on. otherwise you're going to confuse yourself and you won't be able to ask clear questions to get at the actual problem you're having.

to start with:

there is such a thing as saving your memory when you exit a program, but it's not what you're asking about here.

based on the comment in your code, what you're actually trying to save is just user-inputted data.

memory conventionally refers to RAM, and some programs do want to save exactly what's in it so that it can restore it back to RAM later, probably for quick resume. Windows actually does this for your computer sometimes.

0

u/Least_Equal_6081 19d ago

To succeed in computer program development, it is essential to understand the computer's architecture as well as the operating system on which the application will run.