0

While creating a tkinter GUI application, I created a window with a button to close the program and terminate the script, but when I used it, the program did close but the script was still running in the background and I was unable to start the program again.

I have tried several different functions to close the program, but nothing has worked. Here are some of my attempts:

def Cerrar():
    summary.destroy()


No = customtkinter.CTkButton(summary, text="Cerrar", command=Cerrar,width=10)

or

No = customtkinter.CTkButton(summary, text="Cerrar", command=lambda:Cerrar(),width=10)

or

No = customtkinter.CTkButton(summary, text="Cerrar", command=lambda:summary.destroy(),width=10)

here is the window im having a problem with:

def Analisis():
    global resumen,cuantia,summary,refuerzo
    Denom_Malla = "{}-{}".format(Malla.get(),Separacion.get())
    
    denominacion_mallas = ["4-25","4-20","4-15","4.5-15","5-15","5.5-15",
                           "6-15","6.5-15","7-15","7.5-15","8-15","8.5-15",
                           "4-12.5","4-10","4-7.5","4.5-7.5","5-7.5","5.5-7.5",
                           "6-7.5","6.5-7.5","7-7.5","7.5-7.5","8-7.5","8.5-7.5"]
    if Denom_Malla in denominacion_mallas:
        fc = int(Fc.get())
        fy_malla = int(Fy_Malla.get())
        fy_barra = int(Fy_Barra.get())
        mu = float(Mu.get())
        H = float(h.get())    
        Rec = float(rec.get())
        malla = Malla.get()
        separacion = Separacion.get()
        denom_barra = int(Denom_Barra.get())
        resumen, cuantia_calculada, d, Rn, cuantia_min, cuantia_diseño, As,Sep_Barra = Calculo_Losa(fc, fy_malla, fy_barra, mu, H, Rec, malla, separacion, denom_barra)
        root.destroy()
        if resumen == None:
            root_losas(lista_fc.index(str(fc)),lista_fy.index(str(fy_malla)),lista_fy.index(str(fy_barra)),lista_mallas.index(malla),lista_denombarra.index(str(denom_barra)),mu,H,Rec,lista_separacion.index(str(separacion)))
        else:
            summary = customtkinter.CTk()
            summary.title("Resumen")
            summary.minsize(width=300,height=150)
            customtkinter.CTkLabel(summary, text="Momento Último calculado es de: {} KN-m".format(mu)).grid(row=0,column=0,columnspan=2)
            customtkinter.CTkLabel(summary, text="La cuantía calculada es de: {}".format(round(cuantia_diseño,4))).grid(row=1,column=0,columnspan=2)
            customtkinter.CTkLabel(summary, text="Para la losa se necesita una malla {}".format(resumen)).grid(row=2,column=0,columnspan=2)
            customtkinter.CTkLabel(summary, text="Desea realizar otro análisis?").grid(row=3,column=0,columnspan=2)
            customtkinter.CTkLabel(summary, text="En que tipo de refuerzo desea guardar: ").grid(row=2,column=3)
            refuerzo = customtkinter.StringVar(value=lista_refuerzo[0])
            refuerzo_Box = customtkinter.CTkOptionMenu(summary, variable=refuerzo, values=lista_refuerzo, width=100)
            refuerzo_Box.grid(row=3,column=3,padx=15)
            refuerzo_Box.configure(cursor="hand2")
            Yes = customtkinter.CTkButton(summary, text="Analizar", command=lambda:Siguiente(fc,fy_malla,fy_barra,malla,denom_barra,mu,H,Rec,separacion),width=10)
            Yes.grid(row=4,column=0)
            Yes.configure(cursor="hand2")
            Guardar = customtkinter.CTkButton(summary, text="Guardar", command=lambda:Save(fc,fy_malla,mu,1,H,Rec,d,Rn,cuantia_calculada,cuantia_min,cuantia_diseño,As,malla,separacion,denom_barra,Sep_Barra,resumen))
            Guardar.grid(row=4,column=3,pady=15)
            Guardar.configure(cursor="hand2")
            No = customtkinter.CTkButton(summary, text="Cerrar", command=Cerrar,width=10)
            No.grid(row=4,column=1)
            No.configure(cursor="hand2")
            summary.mainloop()
    else:
        tk.messagebox.showerror(title="Error Malla", message="La denominacion de la malla elegida no está registrada.")
            `
JRiggles
  • 4,847
  • 1
  • 12
  • 27
  • 1
    Have you tried using the `summary.quit()` instead of `destroy`? For what it's worth, `command=summary.quit` should work and avoids the need for `lambda`s or other function definitions. – JRiggles Feb 06 '23 at 15:43
  • Yes! When using the `summary.quit()` command, the window doesn't even disappear. – Mateo Gomez Feb 06 '23 at 16:20
  • So the window does close right? You could just do what you're doing now then do exit() or quit() or raise SystemExit to exit the py script instead – mrblue6 Feb 06 '23 at 16:21
  • .destroy() only destroys the window/object it was called on, but doesnt exit the script. In your case, it destroys the summary window but it wont do anything with the script – mrblue6 Feb 06 '23 at 16:22
  • @mrblue6 Thanks!! the `raise SystemExit` worked to terminate the script. – Mateo Gomez Feb 06 '23 at 16:36
  • 2
    I think you need to provide some more code to test with. Looking at your posted code you have both `root` and `summary`. You should never have 2 instances of Tk and if customtkinter is just importing `tk` in its code then that is a problem here. If you can remove all the excess code that is not important and provide a testable example we can better solve your problem. You should never need to use `raise SystemExit` with a Tkinter application. `destroy()` should be sufficient. Thus this leads me to believe something else is wrong in your code. – Mike - SMT Feb 06 '23 at 18:41
  • See [this question](https://stackoverflow.com/questions/75375041/continue-executing-function#comment133010216_75375041). Create a window like ```summary = customtkinter.CTkToplevel()``` and do like ```summary.destroy(); summary.quit()``` in the ```Cerrar()```. And remove the ```root.destroy()```. – relent95 Feb 08 '23 at 03:25

0 Answers0