Вы можете удалить бесконечное l oop, которое блокирует mainloop()
и вызывает зависание программы, и использовать вызов .after()
.
.after
имеет два основных атрибута, задержку и функция, поэтому root.after(time, function)
вернет запланированное задание.
Это задание можно сохранить как глобальную переменную в используемом стиле и отменить с помощью функции остановки.
Ради не постоянно настраивая state
кнопок, я добавил отдельную функцию запуска.
Пример:
from tkinter import filedialog
import tkinter as tk
import time
import os
global dateiListe
def browse_button():
global pfad
global dateiname
dateiname = filedialog.askdirectory()
pfad.set(dateiname)
if len(dateiname) > 0:
print( len(dateiname) )
btn_schleifeStart['state'] = tk.NORMAL
else:
print( len(dateiname) )
btn_schleifeStart['state'] = tk.DISABLED
def start_schleife():
btn_ordnerWählen['state'] = tk.DISABLED
btn_schleifeStart['state'] = tk.DISABLED
btn_schleifeStop['state'] = tk.NORMAL
run_schleife() # Calls run function
def run_schleife():
global job # Make job variable global
dateiListe = []
for datei in os.listdir(dateiname):
if datei.lower().endswith(('.png', '.jpg', '.jpeg')):
listBox.insert(1, datei)
listBox.insert(2, datei)
print(datei)
job = root.after(5000, run_schleife) # schedules a job to run the function again after 5000ms.
def stop_schleife():
# Added a function to stop
root.after_cancel(job) # cancels the scheduled job.
btn_ordnerWählen['state'] = tk.NORMAL
btn_schleifeStart['state'] = tk.NORMAL
btn_schleifeStop['state'] = tk.DISABLED
root = tk.Tk()
root.geometry("500x400")
pfad = tk.StringVar()
btn_ordnerWählen = tk.Button(text="Ordner wählen", command=browse_button)
btn_schleifeStart = tk.Button(text="Start", command=start_schleife,state=tk.DISABLED)
btn_schleifeStop = tk.Button(text="Stop", command=stop_schleife,state=tk.DISABLED) # Added stop button
txt_pfad = tk.Label(master=root,textvariable=pfad, fg="blue")
listBox = tk.Listbox(root)
btn_ordnerWählen.grid(row=0, column=0, sticky="sw")
txt_pfad.grid(row=1, column=0)
btn_schleifeStart.grid(row=3, column=0)
btn_schleifeStop.grid(row=4, column=0) # Grid stop button
listBox.grid(row=5, column=0)
root.mainloop()
Если вы заинтересованы в использовании потоков
Вот как Я подхожу к этому:
from tkinter import filedialog
import tkinter as tk
import time
import os
import threading # import threading
global dateiListe
def browse_button():
global pfad
global dateiname
dateiname = filedialog.askdirectory()
pfad.set(dateiname)
if len(dateiname) > 0:
print( len(dateiname) )
btn_schleifeStart['state'] = tk.NORMAL
else:
print( len(dateiname) )
btn_schleifeStart['state'] = tk.DISABLED
def start_schleife():
global job # Declare global variable to hold Thread
btn_ordnerWählen['state'] = tk.DISABLED
btn_schleifeStart['state'] = tk.DISABLED
btn_schleifeStop['state'] = tk.NORMAL
job = threading.Thread(target = run_schleife) # Create thread object
job.run_thread = True # Sets the "run_thread" attribute to True
job.start() # Start Thread
def run_schleife():
thread = threading.current_thread()
while True:
dateiListe = []
for datei in os.listdir(dateiname):
if datei.lower().endswith(('.png', '.jpg', '.jpeg')):
listBox.insert(1, datei)
listBox.insert(2, datei)
print(datei)
for x in range(50): # 5 second wait, 50 x 0.1 seconds = 5 seconds
if not getattr(thread, "run_thread"): # Checks "run_thread" attribute
print("run_schleife thread stopped")
return
time.sleep(0.1)
def stop_schleife():
# Added a function to stop
job.run_thread = False # Sets the thread's "run_thread" attribute to False
btn_ordnerWählen['state'] = tk.NORMAL
btn_schleifeStart['state'] = tk.NORMAL
btn_schleifeStop['state'] = tk.DISABLED
root = tk.Tk()
root.geometry("500x400")
pfad = tk.StringVar()
btn_ordnerWählen = tk.Button(text="Ordner wählen", command=browse_button)
btn_schleifeStart = tk.Button(text="Start", command=start_schleife,state=tk.DISABLED)
btn_schleifeStop = tk.Button(text="Stop", command=stop_schleife,state=tk.DISABLED) # Added stop button
txt_pfad = tk.Label(master=root,textvariable=pfad, fg="blue")
listBox = tk.Listbox(root)
btn_ordnerWählen.grid(row=0, column=0, sticky="sw")
txt_pfad.grid(row=1, column=0)
btn_schleifeStart.grid(row=3, column=0)
btn_schleifeStop.grid(row=4, column=0) # Grid stop button
listBox.grid(row=5, column=0)
root.mainloop()
Похоже, что добавление / добавление списка потребует некоторой работы, но я не совсем уверен, какова цель, поэтому я оставлю это на другой раз.