Python Forum
Login program - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: Login program (/thread-39361.html)



Login program - Leventeand1 - Feb-05-2023

Hello! I learned python a month ago. I would like to make a login program for practice (GUI). I got to the point where I did the buttons and stuff. But when I wanted to make it so that if there is nothing entered for the username or password, then it is not possible to continue. But it didn't work :(
I tried to do it in the command of the button:

def state():
     if username == None:
         button.config(state=DISABLED)
This didn't work...

The codes for the button:
button = Button(username_frame, text="Login", command=login)
button.grid(row=2, column=0)
My whole code:
https://paste.gg/p/Leventeand1/f5c8e8776f264c63b25042fcf802a131


RE: Login program - CodeCanna - Feb-13-2023

This code seems to have the desired effect you are looking for. I'm not sure if nested while loops are the best way to go but for learning purposes it will do.

I referenced this on stack overflow to modify your state function.

Hope this helps :)

from tkinter import *
	
 
	
def state():
    # Use the length of string returned from username.get() to detect
    # an empty state.
    if len(username.get()) == 0:
        # button.config(state=DISABLED)
        return False
    return True
	
 
	
def login():
	
    new_window = Toplevel()
	
    new_window.title("Logged in as: "+username.get())
	
old_window = Tk()
	
old_window.geometry("400x100")
	
 
	
username = Entry(old_window, font=('Arial',13),
	
                 relief=SOLID)
	
username_label = Label(old_window, text="Username: ").grid(row=0, column=0)
	
username_frame = Frame(old_window).grid(row=2, column=0)
	
 
	
button = Button(username_frame, text="Login", command=login)
	
button.grid(row=2, column=0)
	
username.grid(row=1, column=0)
	
 
	
password_label = Label(old_window, text="Password: ").grid(row=0, column=1)
	
password = Entry(old_window, font=('Arial', 13),
	
                 relief=SOLID)
	
password.grid(row=1, column=1)

# This nested while loop seems to give the desired behavior.
# I'm not totally sure if this is good practice though ;)
# The button should gray-out when all text from the username field is
# deleted and return to normal once input is entered.	
while not state():
    button.config(state=DISABLED)
    old_window.update()
    while state():
        button.config(state=NORMAL)
        old_window.update()

old_window.mainloop()