Tom
Tom

Reputation: 15

Tkinter Password System

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 ?

Upvotes: 1

Views: 19650

Answers (4)

Parsa.kh
Parsa.kh

Reputation: 1

[from tkinter import *
from  tkinter import messagebox
admin=Tk()
admin.geometry("600x600")
admin.resizable(width=False,height=False)
admin.title("admin")
Label(admin,text="enter pasworld").place(x=15,y=25)
 Label(admin,text="show").place(x=125,y=60)
e1=Entry(admin,width=20,textvariable=pas1,show="*")
e1.place(x=105,y=27)
def show():
    if e1.cget('show')=='*':
        e1.config(show='')
    else:
        e1.config(show="*")
c1=Checkbutton(admin,command=show)
c1.place(x=160,y=60)
pass="1234"
def lock():
    old2=old.get()
    pas2=pas1.get()
    if pas2==b:
       messagebox.showinfo("T","acept")
     else:
        messagebox.showinfo("F","Access Denied")][1]

Upvotes: 0

Ivan Aroa Roy
Ivan Aroa Roy

Reputation: 11

import tkMessageBox
from Tkinter import *
def show_entry_fields():
    # print("Nombre : %s\nApellido: %s" %(e1.get(), e2.get()))
    if e1.get() == 'ivan' and e2.get() == '123456':
        tkMessageBox.showinfo(message="Bienvenido Ivan")
    else:
        print "Usuario Incorrecto..."

master = Tk()
Label(master, text="Nombre").grid(row=0)
Label(master, text="Apellido").grid(row=1)

e1 = Entry(master)
e2 = Entry(master, show="*")
e1.insert(10, "Jonas")
e2.insert(10, "reyes")

e1.grid(row=0, column=1)
e2.grid(row=1, column=1)

Button(master, text='Quit', command=master.quit).grid(row=3, column=0, sticky=W, pady=4)
Button(master, text='Show', command=show_entry_fields).grid(row=3, column=1, sticky=W, pady=4)
mainloop()

Upvotes: 1

João Paulo
João Paulo

Reputation: 6670

Here is a simple example how you can check:

from tkinter import *

root = Tk()

username = "abc" #that's the given username
password = "cba" #that's the given password

#username entry
username_entry = Entry(root)
username_entry.pack()

#password entry
password_entry = Entry(root, show='*')
password_entry.pack()

def trylogin(): #this method is called when the button is pressed
    #to get what's written inside the entries, I use get()
    #check if both username and password in the entries are same of the given ones
    if username == username_entry.get() and password == password_entry.get():
        print("Correct")
    else:
        print("Wrong")

#when you press this button, trylogin is called
button = Button(root, text="check", command = trylogin) 
button.pack()

#App starter
root.mainloop()

Look this little preview:

enter image description here

About entry. About button.


It's not a good practice do this code in this way. OOP is recommended. But I'm just trying to show you the resources.

Upvotes: 2

Padraic Cunningham
Padraic Cunningham

Reputation: 180401

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.

Upvotes: 3

Related Questions