Python Tkinter, can I limit the number of entry?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Python Tkinter, can I limit the number of entry?



How can I limit the number of user inputs such that if the user has wrongly tried 5 times already, the system will end immediately?



I have tried to use While loop as usual to achieve. However, the while loop seems to be unable to calculate the number of incorrect input.



Please let me know what should I do. Appreciate


import tkinter as tk
import csv
from tkinter import messagebox


def user_login():
locals()
entry_user_name = entry_usr_name.get()
entry_user_pwd = entry_usr_pwd.get()

with open(r'user.csv', 'r') as user_file
reader = csv.reader(user_file)
user = list(reader)
found = False
count1 = 5
for count in range(len(user)):
if entry_user_name == user[count][0]:
if entry_user_pwd == user[count][1]:
target = count
found = True
break
else: found = False
else: found = False
count1 -= 1
if not found:
tk.messagebox.showerror(title='Error',
message='Invalid password. Please enter again. You have '+str(count1)+ ' left')
if count1 == 0:
tk.messagebox.showerror(title='Error',
line1='Too many input. The system will be ended immediately',
line2='This will be recorded in the security system')
if found:
tk.messagebox.showinfo(title='Successful login',
message='Valid password.')
startpage()



window = tk.Tk()
entry_username = tk.Entry(window, font='Courier 17')
entry_pwd = tk.Entry(window,font='Courier 17')
entry_usr_name.place(x=330, y=245)
entry_usr_pwd.place(x=330, y=345)

login_button= tk.Button(window,
command=user_login) # with some setting
login_button.place(x=440, y=420)
window.mainloop()





show me how you tried to implement the while loop
– Kostadin Slavov
Aug 8 at 13:22





This is just basic logic. Every time they submit the inputs, update a counter. Then, check the value of the counter and call an appropriate function if the number is too big.
– Bryan Oakley
Aug 8 at 13:22





Please wait a minute. I am now updating with my code.
– 7vinBB
Aug 8 at 13:34





I am confused where should I put my while loop in
– 7vinBB
Aug 8 at 13:57





You don't need a while loop.
– Mike - SMT
Aug 8 at 14:10




1 Answer
1



You can use a tracking variable to keep record of how many attempts a person has made.



Here is a simple example.


import tkinter as tk


root = tk.Tk()

counter = 1 # tracking variable

tk.Label(root, text="User Name: ").grid(row=0, column=0)
tk.Label(root, text="Password: ").grid(row=1, column=0)
entry_name = tk.Entry(root)
entry_pass = tk.Entry(root)
entry_name.grid(row=0, column=1)
entry_pass.grid(row=1, column=1)


def check_login():
global counter, entry_name, entry_pass
x = entry_name.get()
y = entry_pass.get()
if counter <= 5: # if counter 5 or less then check login info
if x == "Bob" and y == "mypassword":
print("Correct!")
else:
counter += 1 # add 1 to counter
print("Bad login! Please try again")
else:
print("Exceeded max attempt.")

tk.Button(root, text="Login", command=check_login).grid(row=2, column=0, columnspan=2)
root.mainloop()





Appreciate. You remind me that I have wrongly placed the tracking variable.
– 7vinBB
Aug 8 at 14:24





@7vinBB keep in mind that while loops and Tkinter tend to not work together. Tkinter is event driven and uses mainloop() to manage the events. If you get stuck in a while loop you end up blocking the mainloop().
– Mike - SMT
Aug 8 at 14:28


mainloop()


mainloop()






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS