Я почти уверен, что код довольно беспорядочный. , так как это мой первый проект, извините за это, но такое ощущение, что я не могу разобраться в этом самостоятельно, возясь на многих уровнях.
Я попробовал разбираюсь с функцией mainloop и обратным вызовом списка, но, похоже, я просто упускаю что-то основное.
Код: Выделить всё
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()
С уважением, Роб
Подробнее здесь: https://stackoverflow.com/questions/786 ... ter-window