[Python] Tkinter window does not close bug when closing window while another program is running

Discussão em 'Python' iniciado por Stack, Março 26, 2025.

  1. Stack

    Stack Membro Participativo

    I have file A and file B, file A is a script running and file B uses Tkinter to show an interface. When I am finished with the interface, I close it to return a value to my command line app (run in file A). But even though the script and also the Tkinter window from file B is closed (destroyed, quit, withdraw), the window still appears. However, file B still returns the value I want which means it is in fact closed but somehow the Tkinter window is still there.

    This is some of the code from file A:

    from user_interface import on_call
    print(on_call())


    This is the code from file B:

    def on_drop_resume(event):
    resume_path.set(event.data)
    resume_label.config(bg="#D3D3D3", text="Resume file dropped", state="disabled")
    upload_resume_btn.config(state="disabled") # Disable upload button after drop


    def on_drop_job_description(event):
    job_desc_path.set(event.data)
    job_desc_label.config(bg="#D3D3D3", text="Job Description file dropped", state="disabled")
    upload_job_desc_btn.config(state="disabled") # Disable upload button after drop


    def generate_resume():
    try:
    resume_file = resume_path.get()
    job_desc_file = job_desc_path.get()
    resume_file = resume_file.strip("{}")
    job_desc_file = job_desc_file.strip("{}")

    if resume_file and job_desc_file:
    root.withdraw()
    root.quit()
    root.destroy()
    else:
    print("Both files need to be dropped before generating.")
    except Exception as e:
    print(e)


    def undo_drop_resume():
    try:
    resume_path.set("")
    resume_label.config(bg="white", text="Drag and drop\nResume file here", state="normal")
    upload_resume_btn.config(state="normal")
    except Exception as e:
    print(e)


    def undo_drop_job_description():
    try:
    job_desc_path.set("")
    job_desc_label.config(bg="white", text="Drag and drop\nJob Description file here", state="normal")
    upload_job_desc_btn.config(state="normal")
    except Exception as e:
    print(e)


    def upload_resume():
    file_path = filedialog.askopenfilename(filetypes=[("PDF Files", "*.pdf"), ("Text Files", "*.txt")])
    if file_path:
    resume_path.set(file_path)
    resume_label.config(bg="#D3D3D3", text="Resume file uploaded", state="disabled")
    upload_resume_btn.config(state="disabled")


    def upload_job_description():
    file_path = filedialog.askopenfilename(filetypes=[("PDF Files", "*.pdf"), ("Text Files", "*.txt")])
    if file_path:
    file_path = file_path.strip("{}")
    job_desc_path.set(file_path)
    job_desc_label.config(bg="#D3D3D3", text="Job Description file uploaded", state="disabled")
    upload_job_desc_btn.config(state="disabled")


    def drop_all():
    undo_drop_resume()
    undo_drop_job_description()


    def on_call():
    global root # Reference the root window globally

    root = TkinterDnD.Tk() # Create the root window

    root.title("Resume Builder")
    root.configure(bg="#ADD8E6")

    # Initialize StringVars after creating the root window
    global resume_path, job_desc_path # Declare these as global so they can be accessed in other functions
    resume_path = tk.StringVar(root)
    job_desc_path = tk.StringVar(root)

    frame = tk.Frame(root, bg="#ADD8E6")
    frame.pack(pady=10)

    button_frame = tk.Frame(root, bg="#ADD8E6")
    button_frame.pack(pady=10)

    global upload_resume_btn, resume_label, upload_job_desc_btn, job_desc_label # Declare these as global so they can be accessed in other functions

    upload_resume_btn = tk.Button(frame, text="Upload Resume", command=upload_resume, bg="#D3D3D3", relief="flat", highlightbackground="#ADD8E6")
    upload_resume_btn.grid(row=0, column=0, pady=5)

    resume_label = tk.Label(frame, text="Drag and drop\nResume file here", relief="solid", width=30, height=15, bd=1, bg="white")
    resume_label.grid(row=1, column=0, padx=10)
    resume_label.drop_target_register(DND_FILES)
    resume_label.dnd_bind('<<Drop>>', on_drop_resume)

    upload_job_desc_btn = tk.Button(frame, text="Upload Job Description", command=upload_job_description, bg="#D3D3D3", relief="flat", highlightbackground="#ADD8E6")
    upload_job_desc_btn.grid(row=0, column=1, pady=5)

    job_desc_label = tk.Label(frame, text="Drag and drop\nJob Description file here", relief="solid", width=30, height=15, bd=1, bg="white")
    job_desc_label.grid(row=1, column=1, padx=10)
    job_desc_label.drop_target_register(DND_FILES)
    job_desc_label.dnd_bind('<<Drop>>', on_drop_job_description)

    generate_btn = tk.Button(button_frame, text="Generate Resume", command=generate_resume, relief="flat", highlightbackground="#ADD8E6")
    generate_btn.grid(row=0, column=0, padx=0)

    drop_all_btn = tk.Button(button_frame, text="Drop Files", command=drop_all, relief="flat", highlightbackground="#ADD8E6")
    drop_all_btn.grid(row=0, column=1, padx=0)

    button_frame.configure(bg="#ADD8E6")

    # Close the root window when the user clicks the close button
    root.protocol("WM_DELETE_WINDOW", lambda: root.quit()) # Handle window close event properly

    root.mainloop() # Start the event loop for the main window

    return resume_path.get(), job_desc_path.get()


    I already tried root.withdraw(), root.quit() and also root.destroy(). I even tried to make a new layer window, stacking on top of each other, the top being my UI and the bottom being a hidden base layer. But still, when I close and quit the base layer, the top layer UI still appearing.

    Continue reading...

Compartilhe esta Página