Reputation: 307
from tkinter import *
from PIL import Image,ImageTk
import os
def login_success():
global success_win
success_win = Tk()
success_win.title('Welcome')
success_label = Label(success_win,text='You logged in successfully')
success_label.pack(pady=10)
exitbtn = Button(success_win,text='OK',command=success_win.destroy)
exitbtn.pack(pady=10)
def forget_password():
global forwin
forwin = Tk()
forwin.title('Verification')
Verify_label = Label(forwin,text='Put your phone number',fg='red')
Verify_label.pack(pady=10)
phone_no = IntVar()
Verify_entry = Entry(forwin,width=20,textvariable=phone_no)
Verify_entry.pack(pady=10)
Submit_btn = Button(forwin,text='Submit',command=reset_pw)
Submit_btn.pack(pady=10)
def reset_pw():
Label(forwin,text='The code has been sent to your mobile').pack()
Button(forwin,text='Login',command=login).pack()
def login_verify():
username1 = user_login_entry.get()
password1 = pw_login_entry.get()
pw_login_entry.delete(0,END)
list_of_files = os.listdir()
username_file = username1+'.txt'
if username_file in list_of_files:
times_left = 5
right_input = False
file1 = open(username_file)
verify = file1.read().splitlines()
while not right_input:
if password1 in verify:
login_success()
right_input = True
elif times_left == 0:
reset_pw()
else:
Label(logwin,text='Try again',fg='red').pack()
times_left -= 1
else:
Label(logwin,text='No current user',fg='red').pack()
My aim is to let the wrong password users try five times before go to reset password and when I run the program I enter a wrong password it shows five times at the first time rather than going once each time. May I ask where is the problem? Can tkinter handle one by one using while loop? Any methods that can handle looping but not going all times at the first time?
Upvotes: 0
Views: 189
Reputation: 8037
Try to avoid any infinit loops except the mainloop of tkinter. One will interrupt the other and your program freezes till they are done.
Multiple if-statements and tk.Variables will do this job as well.
import tkinter as tk
import os
def login_verify():
#get your input
username1 = _uv.get()
password1 = _pw.get()
user_lst = os.listdir()
user = username1+'.txt'
#ask for user
user_verify = verify_user(user,user_lst)
if user_verify: #if user is known
#ask for password
pw_verify = verify_password(user,user_lst, password1)
if pw_verify: #password is correct
u_n.delete(0,'end')
p_w.delete(0,'end')
else: #wrong password
p_w.delete(0,'end')
else: #user isnt known
display.configure(text='User not found')
u_n.delete(0,'end')
def verify_user(user, lst):
if user in lst:
print('User in register')
return True
else:
print('User isnt known yet, sign in.')
return False
def verify_password(user,user_lst, pw):
user_f = open(user)
verify = user_f.read().splitlines()
times_left = _pwc.get() #check if there are trys left
if times_left >=1:
if pw in verify:
print('login_success()')
_pwc.set(5) #get another 5 trys after success
return True
else:
display.configure(text='wrong password')
_pwc.set(times_left-1) #minus 1 try
print(_pwc.get())
return False
else:
print('reset_pw()')
root = tk.Tk()
_uv = tk.StringVar()
_pw = tk.StringVar()
_pwc = tk.IntVar() #keep track of trys
_pwc.set(5) #set default
display = tk.Label(text='please sign in') #communicate with user
u_n = tk.Entry(root, textvariable=_uv)
p_w = tk.Entry(root, textvariable=_pw)
b = tk.Button(root, text='verify', command=login_verify)
display.grid(column=0,row=0,columnspan=2)
u_n.grid(column=0,row=1)
p_w.grid(column=1, row=1)
b.grid(column=0, row=2,columnspan=2)
root.mainloop()
Upvotes: 1