0

what my code basically does is copying .txt files from source_path to dest_path, you can test it without all the tkinter code to see that it works. Now, I wanted to make a GUI for this little script I created, and looking at tutorials they tell me that I have to write down root.mainloop() to create a window that stays.

What I want to do is (for now) have this little windows to be alongside my running script, but no matter where I put it it stops my running code and stays at the root.mainloop() forever.

I saw some people use root.after(), but it doesnt seems to work for me. How can I fix this?

import time, shutil, os, datetime
from tkinter import *

root = Tk()                                             # defines tkinter
source_path = r"C:\SOURCE"                              # Your Source path
dest_path = r"C:\DESTINATION"                           # Destination path
file_ending = '.txt'                                    # Needed File ending
files = os.listdir(source_path)                         # lits directorys in source_path
date = datetime.datetime.now().strftime('%d.%m.%Y')     # get the current date
sleepingtime = 10                                       # amount of time sleeping
label = Label(root, text="some text")

while True:   
    label.pack()
    root.mainloop()
    print("Beginning checkup")
    print("=================")
    if not os.path.exists(source_path) or not os.path.exists(dest_path): # checks if directory exists
        print("Destination/Source Path does not exist!")
    else:
        print("Destination exists, checking files...")
        for f in files:
            if f.endswith(file_ending):
                new_path = os.path.join(dest_path, date + " - DO-PC")
                src_path = os.path.join(source_path, f)
                if not os.path.exists(new_path):  # create the folders if they dont already exists
                    os.makedirs(new_path)
                if not os.path.exists(os.path.join(new_path, f)):
                    print("copying " + src_path)
                    shutil.copy(src_path, new_path)
                else:
                    print(src_path + "  already copied")

    print("=================")
    print('Checkup done, waiting for next round...')
    time.sleep(sleepingtime) # wait a few seconds between looking at the directory
diatomym
  • 163
  • 1
  • 2
  • 11
  • 1
    You have written a single-threaded application to run your UI and to work on your files. This won't work. Calling `root.mainloop()` will pass your thread to tk so tk can do its job. – MadMike Dec 02 '16 at 13:37
  • 1
    Is this answer useful to your question? http://stackoverflow.com/a/16747734/406423 – MadMike Dec 02 '16 at 14:34
  • For coding GUIs, you'll want to read about "event driven programming". The key point is to have your code in functions. When the user triggers some kind of event, you invoke one of your functions. – glenn jackman Dec 02 '16 at 18:30

1 Answers1

0

Soo, root.mainloop() will wait for you to press esc to exit the window.

to make the script work without pause. you can use root.update() to update you code and the script will work fine.