Home > Software design >  My program freezes until the CMD command finishes ( Python 3 )
My program freezes until the CMD command finishes ( Python 3 )

Time:08-20

when I run my program and press a button, the function it has to do is performed, but the problem is that I cannot use the program until the function of the button I press has finished, it stays in "program does not respond", once the function of the pressed button is finished I can use the program normally

import os 
from time import sleep
import PySimpleGUI as sg 
import subprocess
sg.theme('DarkAmber')
button_size = (16, 1)
layout = [[sg.Text("RECUERDE EJECUTAR EL PROGRAMA COMO ADMINISTRADOR",key="-WINNER-")], [sg.Button("HERRAMIENTA SFC", key="SFC", size=(button_size)), sg.Button("HERRAMIENTA DISM", key="-DISM-"), sg.Button("VER LICENCIA ", key="-KEY-",size=(button_size))],
                [sg.Text("", key="-AVISO-")],
                [sg.Button("Cerrar Programa", key="-OK-")]]

window = sg.Window("PROGRAMA CORREGIR SISTEMA OPERATIVO", layout) 







 def main(window):
    while True:
        event, values = window.read()  
        if event == sg.WIN_CLOSED or event == "-OK-":
            break 

        # Acá estan las herramientas que solucionen problemas del SISTEMA OPERATIVO
        if event == "SFC":
            sg.popup("Se ejecutará la herramienta DISM El proceso seguirá aunque el programa parezca congelado") 
            window.Element("-AVISO-").Update("Se ejecutará la herramienta SFC /scannow El proceso seguirá aunque el programa parezca congelado")
            os.system("sfc /scannow") 
            window.Element("-AVISO-").Update("Ha finalizado...")  

        if event == "-DISM-":
            sg.popup("Se ejecutará la herramienta DISM El proceso seguirá aunque el programa parezca congelado") 
            window.Element("-AVISO-").Update("Se ejecutará la herramienta DISM El proceso seguirá aunque el programa parezca congelado")
            os.system("DISM /Online /Cleanup-Image /CheckHealth")
            sg.popup_auto_close('ETAPA 1/3 FINALIZADO, el programa parecerá congelado pero el proceso seguirá.')
            os.system(" DISM /Online /Cleanup-Image /ScanHealth")
            sg.popup_auto_close('ETAPA 2/3 FINALIZADO, el programa parecerá congelado pero el proceso seguirá.')
            os.system("DISM /Online /Cleanup-Image /RestoreHealth") 
            window.Element("-AVISO-").Update("Etapa 3/3 finalizada, recomendamos usar SFC SCANNOW y/o reiniciar la PC") 


        if event == "-KEY-":
            sg.popup("A continuación WINDOWS verificará el estado de tu licencia")
            subprocess.call("slmgr/dli", shell=True)
            sg.popup("Proceso finalizado")
            
if __name__ == "__main__":
    main(window)

CodePudding user response:

Almost all GUI frameworks are based on an event loop; that would be the while True and event, values = window.read() in your code. Nothing in the GUI will respond unless that loop is running in a timely fashion. Anything that blocks the loop, such as os.system or subprocess.call will make the GUI hang for a while.

  • Related