Home > Software design >  How to stop my entire python program from closing?
How to stop my entire python program from closing?

Time:11-07

I am new to Python, so after making simple programs I am making a program. Using Tkinter I have managed to create the main screen and login screen(login form GUI). The functionality works however, if I do not want to log in and click on the back button, it should close the login screen(login form GUI), but it closes the entire program.

How do I avoid that?

Here is how I created the application.

class LoginFrame(ttk.Frame):
    def __init__(self, container):
        super().__init__(container)
        self.__createMain__()

    # Creates the widgets for the Main Screen
    def __createMain__(self):

        #Some Labels and entry buttons code

        Button2 = Button(self, text='Close', command=self.Closes).place(x=200, y=140)


    # Closes the main program
    def Closes(self):
        quit()


class Login(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title('Login Form')
        self.geometry('350x300')
        self.minsize(200,300)
        self.maxsize(500,500)
        self.__create_widgets()

    def __create_widgets(self):
        # create the input frame
        First_Frame = LoginFrame(self)
        First_Frame.place(height=200, width=700, x=20, y=20)

I am calling the Login class to my main program through a command, it opens up but I cannot close it. How can I do it?

CodePudding user response:

Use this code.

This will close the full login screen.


class LoginFrame(ttk.Frame):
    def __init__(self, container):
        self.con = container
        super().__init__(container)
        self.__createMain__()

    # Creates the widgets for the Main Screen
    def __createMain__(self):

        #Some Labels and entry buttons code

        Button2 = Button(self, text='Close', command=self.Closes).place(x=200, y=140)


    # Closes the main program
    def Closes(self):
        self.con.destroy()


class Login(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title('Login Form')
        self.geometry('350x300')
        self.minsize(200,300)
        self.maxsize(500,500)
        self.__create_widgets()

    def __create_widgets(self):
        # create the input frame
        First_Frame = LoginFrame(self)
        First_Frame.place(height=200, width=700, x=20, y=20)





But if you want to close the LoginFrame Only then change closes function to this.

# Closes the main program
def Closes(self):
    self.destroy()
  • Related