Issue
I'm fairly new to using Tkinter in python and I'm wondering if anyone can help me with my password system. I'm struggling when it comes to checking the user entered username with the actual username, I'm not too bothered about the password part at this point as once I have got the username part working it should be easy enough just to slightly adjust the code from the username section. Anyway here is my code:
#Toms Password system
import tkinter
from tkinter import *
from tkinter import ttk
username = ("Tom")
password = ("")
usernameguess1 = ("")
passwordguess1 = ("")
#ignore the file writing part,I'll sort this out later.
file = open("userlogondata.txt", "w")
file.write("User Data:\n")
file = open("userlogondata.txt", "r")
def trylogin():
print ("Trying to login...")
if usernameguess == username:
print ("Complete sucsessfull!")
messagebox.showinfo("-- COMPLETE --", "You Have Now Logged In.",icon="info")
else:
print ("Error: (Incorrect value entered)")
messagebox.showinfo("-- ERROR --", "Please enter valid infomation!", icon="warning")
#Gui Things
window = tkinter.Tk()
window.resizable(width=FALSE, height=FALSE)
window.title("Log-In")
window.geometry("200x150")
window.wm_iconbitmap("applicationlogo.ico")
#Creating the username & password entry boxes
usernametext = tkinter.Label(window, text="Username:")
usernameguess = tkinter.Entry(window)
passwordtext = tkinter.Label(window, text="Password:")
passwordguess = tkinter.Entry(window, show="*")
usernameguess1 = usernameguess
#attempt to login button
attemptlogin = tkinter.Button(text="Login", command=trylogin)
usernametext.pack()
usernameguess.pack()
passwordtext.pack()
passwordguess.pack()
attemptlogin.pack()
#Main Starter
window.mainloop()
What am I doing wrong that doesn't allow me to check if the user name is correct ?
Solution
You need to get
the value from the Entry widget:
if usernameguess.get() == username:
You should also import what you need and use underscores in your variable names:
from tkinter import messagebox, Label, Button, FALSE, Tk, Entry
username = ("Tom")
password = ("")
def try_login():
print("Trying to login...")
if username_guess.get() == username:
messagebox.showinfo("-- COMPLETE --", "You Have Now Logged In.", icon="info")
else:
messagebox.showinfo("-- ERROR --", "Please enter valid infomation!", icon="warning")
#Gui Things
window = Tk()
window.resizable(width=FALSE, height=FALSE)
window.title("Log-In")
window.geometry("200x150")
#Creating the username & password entry boxes
username_text = Label(window, text="Username:")
username_guess = Entry(window)
password_text = Label(window, text="Password:")
password_guess = Entry(window, show="*")
#attempt to login button
attempt_login = Button(text="Login", command=try_login)
username_text.pack()
username_guess.pack()
password_text.pack()
password_guess.pack()
attempt_login.pack()
#Main Starter
window.mainloop()
You also never close your file after opening for writing so there is a good chance you will experience behaviour you would not expect, close your files after you are finished with them or better again use with
to open them.
Answered By - Padraic Cunningham
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.