Elements in function won’t seem to work after first activation (python)

For fun, I am setting up a messaging system with usernames and passwords. as a basic defence against hydra (brute force) attacks, I’ve attempted to implement a timer that activates after 5 incorrect attempts. For an unknown reason, the ‘incorrect username or password’ message does not seem to display, even though I reset the variable each time its loaded.

Expected: after 5 incorrect attempts, program forces user to wait 10 seconds and then resets. if 5 more incorrect attempts occur, the user is forced to wait 20 seconds and so on.

What happens: after 5 incorrect attempts, program forces user to wait 10 seconds and then resets. after reset, anything entered has no effect, even correct inputs.

Relevant code for reference:

#import stuff
import tkinter as tk
import os
from socket import *
import time

#initialize
print("initializing...")
login = False
host = ""
usernameenter = ""
passwordenter = ""
usernames = [""]
passwords = [""]
ipaddressname = [""]
ipaddress = [""]
usersipadname = [""]
usersipad = [""]
sendreceive = 0
antihydratimer = 10
antihydratries = 0
seconds = 0

#open files and assign relevant arrays
try:
    with open("usernames_file.txt", "r") as f:
        usernames = f.readlines()
    with open("passwords_file.txt", "r") as f:
        passwords = f.readlines()
    print("Storage files found. Loading complete.")
# if no files create them
except FileNotFoundError:
    print("No storage files found. Creating files...")
    open("usernames_file.txt", "a").close()
    open("passwords_file.txt", "a").close()

#setup an antihydra timer
def countdown_timer(seconds):
    #globalize variable
    global antihydratimer
    #timer
    while seconds:
        mins, secs = divmod(seconds, 60)
        timer = '{:02d}:{:02d}'.format(mins, secs)
        time.sleep(1)
        seconds -= 1
        
    #end timer and reset page
    print("Time's up!")
    enter_signin()
    antihydratries = 0

#setup screen reset for after user enters new acc data
def reset_screen():
    #idk what this is
    for widget in root.winfo_children():
        widget.destroy()

    #clear entered data and remove labels if they exist
    if 'button3' in globals():
        button3.destroy()
    if 'newusername_label' in globals():
        newusername_label.destroy()
    if 'newpassword_label' in globals():
        newpassword_label.destroy()
    if 'button4' in globals():
        button4.destroy()
    if 'username_label' in globals():
        username_label.destroy()
    if 'password_label' in globals():
        password_label.destroy()

    #set up initial page
    basic_GUI()

#setup new account page
def on_click_signup():
    #globalize relevant variables and tk stuff
    global newusername_label, newpassword_label, newusernameentered, newpasswordentered, button3, signupclicked, newusername_label
    #remove buttons to signin and signup
    button1.pack_forget()
    button2.pack_forget()
    
    #where user enters new username and saves it as 'newusernameentered'
    newusername_label = tk.Label(root, text="New username:", width=40)
    newusername_label.pack()
    newusernameentered = tk.Entry(root)
    newusernameentered.pack(padx=10, pady=10)

    #where user enters new password and saves it as 'newpasswordentered'
    newpassword_label = tk.Label(root, text="New password:", width=40)
    newpassword_label.pack()
    newpasswordentered = tk.Entry(root, show="*")
    newpasswordentered.pack(padx=10, pady=10)

    #enter key to begin transfer of entered data
    button3 = tk.Button(root, text="Enter", width=40, command=enter_signup)
    button3.pack(padx=10, pady=10)

def enter_signup():
    #enters entered data
    newusername = newusernameentered.get()
    newpassword = newpasswordentered.get()
    
    #pretty sure this doesnt work
    if newusername in usernames:
        print("Username already exists.")
        #resets page
        on_click_signup()
        
    #if username is valid
    else:
        #appends to array
        usernames.append(newusername)
        passwords.append(newpassword)

        #appends to .txt file
        with open("usernames_file.txt", "a") as f:
            f.write(newusername + "n")
        with open("passwords_file.txt", "a") as f:
            f.write(newpassword + "n")

        #yay
        print("Account created successfully!")
        print("Your new username is", newusername)
        print("Your new password is", newpassword)

        #destroys signup screen and shows initial screen
        reset_screen()
        #will add home button sometime

#setup entering acc page
def on_click_signin():
    #globalize relevant variables and tk stuff
    global username_label, password_label, usernameentered, passwordentered, button4, signinclicked
    #remove buttons to signin and signup
    button1.pack_forget()
    button2.pack_forget()

    #where user enters username and saves it as 'usernameentered'
    username_label = tk.Label(root, text="Username:")
    username_label.pack()
    usernameentered = tk.Entry(root)
    usernameentered.pack()

    #where user enters password and saves it as 'passwordentered'
    password_label = tk.Label(root, text="Password:")
    password_label.pack()
    passwordentered = tk.Entry(root, show="*")
    passwordentered.pack()

    #enter key to begin transfer of entered data
    button4 = tk.Button(root, text="Enter", width=40, command=enter_signin)
    button4.pack(padx=10, pady=10)

def enter_signin():
    #globalize relevant variables and tk stuff
    global login, antihydratimer, antihydratries, seconds
    #resets variable if giong through process 2nd time
    triestoincorrect = 0
    #enters entered data
    username = usernameentered.get()
    password = passwordentered.get()

    #if timer isnt active
    if seconds == 0:
        #runs loop based on length of usernames array
        for index in range(len(usernames)):
            #if statement regarding if entered username matches with current indexed username and password in array
            if usernames[index] == username and passwords[index] == password:
                #if true will let user in to their contacts
                print("Welcome aboard captain, all systems online.")
                login = True
                #should stop loop
                return
            #if current indexed username and password in array does not match raises triesto incorrect
            else:
                triestoincorrect += 1

        #if all indexed username and password in array does not match, the password or username must not be valid
        if triestoincorrect >= len(usernames):
            #adds 1 to antihydra
            antihydratries += 1
            #if antihydra happens 5 times activate security measures
            if antihydratries >= 5:
                #cumulative timer
                seconds = antihydratimer
                #digital gandalf defeats balrog
                print("YOU SHALL NOT PASS! for", antihydratimer, "seconds")
                #activates countdown timer
                countdown_timer(seconds)
                #adds 10s more for future incidents (you could probalby set a macro to close and reopen but ok)
                antihydratimer += 10
            else:
                print("Incorrect username or password")


root = tk.Tk()

#for after screen reset
def basic_GUI():
    global button1, button2
    button1 = tk.Button(root, text="Sign Up", width=40, command=on_click_signup)
    button1.pack(padx=10, pady=10)
    button2 = tk.Button(root, text="Sign In", width=40, command=on_click_signin)
    button2.pack(padx=10, pady=20)

basic_GUI()

New contributor

Professional_Timewaster is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật