0

The goal of this code is to create a functioning login screen. However, in the process of creating the "create_account()" and implementing it, I noticed that for some reason it triggers as soon as the program runs. Not only that but when the button that the function is linked to is clicked, nothing seems to happen. What gives?

from tkinter import *


counter = 0

def login():

    user = txt.get()
    pwd = txt2.get()

    if user in users:
      if pwd == users[user]:
        lbl3.config(text="Login successful")
      else:
        lbl3.config(text="Invalid username or password")
    else:
      lbl3.config(text="Invalid username or password")

    user = txt.get()
    pwd = txt2.get()
    certificate = 0

    if user in users:
      if pwd == users[user]:
        lbl3.config(text="Login successful")
      else:
        lbl3.config(text="Invalid username or password")
    else:
      lbl3.config(text="Invalid username or password")


def up(string):
    for x in string:
        if(x.upper()):
            return True
        else:
            return False

def low(string):
    for x in string:
        if(x.lower()):
            return True
        else:
            return False

def length(string):
    if(len(string) > 8 and len(string) < 15):
        return True
    else:
        return False

def whitespace(string):
    for x in string:
        if(x.isspace()):
            return False
        else:
            return True

def number(string):
    for x in string:
        if(x.isdigit()):
            return True
        else:
            return False


def alphanumberic(string):
    for x in string:
        if(x.isalnum()):
            return False
        else:
            return True
            

def valid_password(string):
    up(string)
    low(string)
    length(string)
    whitespace(string)
    number(string)
    alphanumberic(string)
    if(string==("BigCodez4life!")):
        return True
        
def valid_username(string):
    up(string)
    low(string)
    whitespace(string)
    number(string)
    alphanumberic(string)
    if(string==("BigCodez4life!")):
        return True
    
        
    if(length(string) == True and whitespace(string) == False and number(string) == True and alphanumberic(string) == False and up(string) == True and low(string) == True):
        return True
    else:
        return False


def create_account():
  username = txt.get()
  password = txt2.get()
  if username != "" and valid_username(username) == True and valid_password(password) == True:
    lbl3.config(text="Account Created")
  else:
    lbl3.config(text="Invalid username or password")


window = Tk()
window.title("Login Window")
window.geometry("480x270")

lbl = Label(window, text="Username")
lbl.grid(column=0, row=0)

lbl2 = Label(window, text="Password")
lbl2.grid(column=0, row=1)

lbl3 = Label(window, text="Hidden")
lbl3.grid(column=1, row=10)
lbl3['foreground']="red"
lbl3['width']=30

txt = Entry(window,width=15)
txt.grid(column=1, row=0)

txt2 = Entry(window,width=15,show="*")
txt2.grid(column=1, row=1)

btn = Button(window, text="Login", command=login)
btn.grid(column=2, row=7)

btn2 = Button(window, text="Create Account", command=create_account())
btn2.grid(column=0, row=7)


users = dict()
realusers = []
pwds = []

window.mainloop()
Bruh
  • 1
  • Few issues: 1) try this: ```python btn2 = Button(window, text="Create Account", command=lambda: create_account()) ``` 2) your txt and txt2 variables are not assigned within the scope. I would suggest putting everything inside a class and then scoping it to "self". If you need clarification let me know, but if you need more of an explanation I can post an answer, but that doesn't seem necessary. – NationWidePants Apr 14 '21 at 14:15

1 Answers1

0

It is because of this line:

btn2 = Button(window, text="Create Account", command=create_account())

command=create_account will run create_account once the line is executed. All you need to do is remove the parenthesis and most likely it will work just fine.

tdserapio
  • 41
  • 1
  • 8