0

The idea is that it searches for a particular file while keeping a loading screen up then closes when the search is complete. This is my current code and it will not continue until the window is closed.

from tkinter import *
import os

def check():
    for root, dirs, files in os.walk ("C:\\Users\\"):
        for file in files:
            if file.endswith("Details.txt"):
                fileLocation=(os.path.join(root, file))
                print(fileLocation)
                loading.destroy()

            else:
                createFile=open("Details.txt", "w")
                createFile.close()
                loading.destroy()
loading = Tk()
text = Label(loading, text="Loading...")
text.pack()
loading.mainloop()
check()
martineau
  • 119,623
  • 25
  • 170
  • 301
odj2001
  • 1
  • 1
  • 2
  • Possible duplicate [tkinter-how-to-use-threads-to-preventing-main-event-loop-from-freezing](https://stackoverflow.com/questions/16745507/tkinter-how-to-use-threads-to-preventing-main-event-loop-from-freezing/16747734#16747734) – stovfl Oct 12 '18 at 20:24
  • I think what would make most sense is to add the check as part of what your tkinter process will do (rather than just show a load screen). – busybear Oct 12 '18 at 20:31
  • Along the lines of what @stovfl is getting at, you could use multi-threading and run the code that does the searching at the same time as the GUI's `mainloop()` is executing (as long as the code doing the search doesn't make any `tkinter` calls itself). – martineau Oct 12 '18 at 21:23
  • *it will not continue until the window is closed.*: This is because `.mainloop()` is **blocking**. to mean, all after, are executed on return from `.mainloop()` which is your *window*. – stovfl Oct 13 '18 at 07:05

1 Answers1

0

Here's how it could be done by making the file searching function run in a separate thread, as I suggested in a comment. It uses a Threading.Event to communicate between the main thread which is running the tkiner GUI, and the thread doing the searching. I also fixed the search function so it properly creates the file when it wasn't found at end.

import os
import sys
import tkinter as tk
from threading import Event, Thread

EVENT_TIMEOUT = .01  # A very short timeout - seconds.
POLLING_DELAY = 1000  # How often to check search status - millisecs.


def file_search(event):
    search_folder = r"C:\Users"
    target = "Details.txt"
    found = False

    for root, dirs, files in os.walk(search_folder):
        for filename in files:
            if filename.endswith(target):  # "if filename == target" would work fine here.
                fileLocation = os.path.join(root, filename)
                print(fileLocation, "file found")
                found = True
                break
        if found:
            break  # Quit looking.

    if not found:
        createFile = open(target, "w")  # Creates empty file in cwd.
        createFile.close()
        fileLocation = os.path.join(os.getcwd(), target)
        print(fileLocation, "file created")

    event.set()  # Signal search has completed.


def check_status(parent, event):
    event_is_set = event.wait(EVENT_TIMEOUT)
    if event_is_set:  # Done searching?
        parent.destroy()
        sys.exit()
    else:  # Continue polling.
        parent.after(POLLING_DELAY, check_status, parent, event)


parent = tk.Tk()
text = tk.Label(parent, text="Loading...")
text.pack()
event = Event()
thread = Thread(target=file_search, args=(event,))
check_status(parent, event)  # Starts the polling of search status.
thread.start()
parent.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301