My problem is the following:
I have a very simple tkinter window, in which I have put a single button. When the user presses this button, a function is executed. This function looks for some files by name and if it doesn't exist it shows a message. The process takes a few minutes, but in the meantime the graphical interface hangs with the famous (not responding). What I need is that while this process is running, the user can continue to use the GUI without problem. I suspect the solution is related to threads but I'm not sure how to do it. Here the code:
from tkinter import *
import os.path
import tkinter as tk
from tkinter import messagebox
import time
def verificacion():
messagebox.showinfo("Exito", "Todos los archivos existen!!")
def mensajeOmitidos(archivos):
messagebox.showinfo("Omitido", archivos)
archivos = ["archivo1.py", "archivo2.py", "archivo3.py"]
# Esta es la funcion llamada con el boton
# En este ejemplo no demora mucho, pero el mio
# necesita conectarse a una base de datos por lo que le toma mas tiempo
def funcionConsulta():
time.sleep(3)
cont = ""
for x in archivos:
if not os.path.isfile(x):
cont = cont + "," + x
if len(cont) != 0:
mensajeOmitidos(cont)
else:
verificacion()
raiz = Tk()
raiz.geometry("500x500")
raiz.resizable(width=False, height=False)
botonConsulta = tk.Button(raiz, text="actualizar", command=funcionConsulta)
botonConsulta.grid(row=5, column=5)
raiz.mainloop()
I leave you a screenshot of my test program, at this point it had been like this for 3 minutes.
I must clarify that the test code does not take as long as the original, since in the latter I must connect to a database and do many more things.
I appreciate any help you can give me, I'm waiting.
1 Answers