Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Login program
#1
Question 
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/f5c8e8776...fcf802a131
Larz60+ write Feb-06-2023, 01:56 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
You can post your entire code within python tags, links may be ignored by many users. (added for you above this time). Please use these tags in future posts.
Reply
#2
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()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020