简体   繁体   中英

How to close tkinter window without a button?

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?

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 >= 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()

Consider for a moment the following two lines in your logging_in method:

if user_get == 'octo' and pass_get == 'burger':
elif user_get != 'octo' or pass_get != 'burger':

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.

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

elif attempts >= 3:

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.

The easiest way to fix this would be to change your

elif attempts >= 3:

line to read

if attempts >= 3:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM