How to close tkinter window without a button?
<p>I'm an A-level computing student and I am the only one in my class who can code using Python. Even my teachers have not learned the language. I'm attempting to code a login program that exits when the info is put in correctly and displays a welcome screen image (I haven't coded that part yet). It has to close and display a fail message after 3 failed login attempts. I've encountered many logic errors when attempting to alter my attempts variable for the elif statement to work after many failed logins as well as getting the tkinter window to terminate/close based on the relevant if/elif statement. This is failing to work and I've looked at many code examples on this site and cannot find anything, could I please get some help with fixing my code?</p>

<p>Code:</p>

<pre class="lang-py prettyprint-override"><code>from tkinter import * #Importing graphics

attempts = 0 #Defining attempts variable

def OperatingProgram(): #Defining active program

    class Application(Frame):
        global attempts
        def __init__(self,master):
            super(Application, self).__init__(master) #Set __init__ to the master class
            self.grid()
            self.InnerWindow() #Creates function

        def InnerWindow(self): #Defining the buttons and input boxes within the window
            global attempts
            print("Booted log in screen")
            self.title = Label(self, text=" Please log in, you have " + str(attempts) + " incorrect attempts.") #Title
            self.title.grid(row=0, column=2)

            self.user_entry_label = Label(self, text="Username: ") #Username box
            self.user_entry_label.grid(row=1, column=1)

            self.user_entry = Entry(self)                        #Username entry box
            self.user_entry.grid(row=1, column=2)

            self.pass_entry_label = Label(self, text="Password: ") #Password label
            self.pass_entry_label.grid(row=2, column=1)

            self.pass_entry = Entry(self)                        #Password entry box
            self.pass_entry.grid(row=2, column=2)

            self.sign_in_butt = Button(self, text="Log In",command = self.logging_in) #Log in button
            self.sign_in_butt.grid(row=5, column=2)

        def logging_in(self):
            global attempts
            print("processing")
            user_get = self.user_entry.get() #Retrieve Username
            pass_get = self.pass_entry.get() #Retrieve Password

            if user_get == 'octo' and pass_get == 'burger': #Statement for successful info
                import time
                time.sleep(2)       #Delays for 2 seconds
                print("Welcome!")
                QuitProgram()
            elif user_get != 'octo' or pass_get != 'burger': #Statement for any failed info
                if attempts &gt;= 2:   #Statement if user has gained 3 failed attempts
                   import time
                   time.sleep(2)
                   print("Sorry, you have given incorrect details too many times!")
                   print("This program will now end itself")
                   QuitProgram()
                else:               #Statement if user still has enough attempts remaining
                    import time
                    time.sleep(2)
                    print("Incorrect username, please try again")
                    attempts += 1
            else:                   #Statement only exists to complete this if statement block
                print("I don't know what you did but it is very wrong.")

    root = Tk() #Window format
    root.title("Log in screen")
    root.geometry("320x100")

    app = Application(root) #The frame is inside the widget
    root.mainloop() #Keeps the window open/running

def QuitProgram(): #Defining program termination
    import sys
    sys.exit()

OperatingProgram()
</code></pre>

<p>Consider for a moment the following two lines in your logging_in method:</p>

<pre><code>if user_get == 'octo' and pass_get == 'burger':
elif user_get != 'octo' or pass_get != 'burger':
</code></pre>

<p>so if the login credentials are correct, the code after the first test is executed. If they are incorrect, the code after the second test is executed.</p>

<p>However, the code that you want to see executed after multiple failures is under a third test clause:</p>

<pre><code>elif attempts &gt;= 3:
</code></pre>

<p>The thing is, the thread of execution will never see this test, as either the first or second one will have already evaluated to true (login credentials are correct, login credentials are incorrect) - it would need to be possible for them both to evaluate to false before the value of attempts would ever be checked.</p>

<p>The easiest way to fix this would be to change your</p>

<pre><code>elif attempts &gt;= 3:
</code></pre>

<p>line to read</p>

<pre><code>if attempts &gt;= 3:
</code></pre>

<p>adjusting the else clause/adding a new one if you feel it is necessary.</p>

python python-3.x tkinter