Reputation: 1
coming from an amateur level of javascript I just started messing around with python and tried to write a small script that checks if a lock file (selected by the user) exists over and over again and notifies if it doesn't. The code more or less works but i can't figure out how to deal with the listbox window. After the selection it freezes until the while loop ends, but I can't find out how to get back to a responding UI even though the code continues correctly. I would like to add a message, notifying the user that the script is now "scanning" with an additional button to quit the script.
I'm pretty sure that the code is pretty messy, as this is kind of my first project, so sorry for that, but it feels like I can't sort it out on my own, messing around on to many levels.
I tried around with the mainloop function and the callback of the listbox but I guess I'm just missing something basic.
import time
import os
import configparser
from tkinter import *
from tkinter import ttk
# importing folder path
config = configparser.ConfigParser()
config.read('config.ini')
projectpath = config.get('DEFAULT', 'path') #to do - check path exists
lckFiles = []
selectedFilePath = ""
winSelection = Tk()
winSelection.geometry('250x300')
winSelection.title('Locked')
listbox = Listbox(winSelection, width=40, height=10, selectmode=SINGLE)
for dirpath, dirnames, filenames in os.walk(projectpath):
for filename in [f for f in filenames if f.endswith(".lck")]:
listbox.insert(END, filename)
lckFiles.append(os.path.join(dirpath, filename))
def processSelection():
global selectedFilePath
selectedFilePath = lckFiles[listbox.curselection()[0]]
if not os.path.exists(selectedFilePath):
print("The file doesn't exist.")
quit()
else:
winSelection.quit()
btnSelect = Button(winSelection, text='Watch', command=processSelection)
btnSelect.pack(side='bottom')
listbox.pack()
winSelection.mainloop()
print("Start watching file...")
print(selectedFilePath)
fileLocked = True
while fileLocked:
if os.path.exists(selectedFilePath):
print("The file is still locked.")
time.sleep(30)
else:
print("The file is not locked anymore.")
fileLocked = False
win = Tk()
win.geometry("240x80")
Label(win, text="The File is unlocked now.", font=('Helvetica 14 bold')).pack(pady=10)
button_close = ttk.Button(win, text="Close", command=win.destroy)
button_close.pack(fill='x')
win.mainloop()
Looking forward for any help. Thanks a lot in advance for your time.
Regards, Rob
Upvotes: 0
Views: 36