Home > Enterprise >  Stopping messagebox from looping the entire textfile after it found a value
Stopping messagebox from looping the entire textfile after it found a value

Time:12-02

i created register and login app. I made a messagebox that pops up when it found a value in the textfiles and also when it doesnt find the value. However it keeps looping my entire textfiles so it loops untill it find the value. How do i prevent it form looping? I tried break but it made it stop at 1st row of textfiles. Please ignore the register button, just the login function at the moment.

the textfiles(users) for login info

from tkinter import *

import time

import tkinter.messagebox as tkMessageBox



##3 

#### Variable

##3



window = Tk()

window.geometry("600x400")

window.title("Bookloaner")

stud = open("users.txt","r")

logdin = False

username = "admin"

password = "admin123"

stud = open("users.txt","r")

books=[]

books = open("books.txt","r")



##3

#### Define

##3



def closer():

    frame.pack_forget()

    logframe.pack_forget()

    regframe.pack_forget()

    extframe.pack_forget()

    time.sleep(0.1)

####2

# FRAMES

####2





logframe=Frame(window)

regframe=Frame(window)

extframe=Frame(window)



def Login():

    closer()

    def Chek():
        
        for line in open("users.txt", "r").readlines():
            loginn_info= line.split()

            if name.get() == loginn_info[1] and passwd.get() == loginn_info[2]:
                tkMessageBox.askokcancel("System","logged",)
                
            else:
                tkMessageBox.askokcancel("System","Error",)
                

    frame.pack_forget()

    conf = StringVar()

    mesg=Entry(logframe, width=30,textvariable=conf,fg="#900",state="readonly",relief=FLAT)

    mesg.grid(row=0,column=2)

    labname=Label(logframe,text="What is your name?")

    labname.grid(row=1, column=1, sticky=E)

    name=StringVar()
    entname=Entry(logframe, textvariable=name)

    entname.grid(row=1, column=2, sticky=W)

    labpass=Label(logframe,text="What is your password?")

    labpass.grid(row=2, column=1, sticky=E)

    passwd=StringVar()
    entpass=Entry(logframe, textvariable=passwd)

    entpass.grid(row=2, column=2, sticky=W)

    checkbtn = Button(logframe, text="Login", fg="#fff", bg="#00f", command=Chek)

    checkbtn.grid(row=3,column=2, sticky=E)

    logframe.pack()





def Register():

    def Chek():

        for i in stud.readlines():

            i.rstrip("\n")

            i = i.split(":")

            print(i)

            if username in i[0]:

                if password in i[1]:

                    print("logged in!")

                    break

                else: print("wrong username or password")

            else: print("User doesn't exist.")

    frame.pack_forget()

    logframe=Frame(window)

    labname=Label(logframe,text="What is your name?")

    labname.grid(row=1, column=1, sticky=E)

    entname=Entry(logframe)

    entname.grid(row=1, column=2, sticky=W)

    labpass=Label(logframe,text="What is your password?")

    labpass.grid(row=2, column=1, sticky=E)

    entpass=Entry(logframe)

    entpass.grid(row=2, column=2, sticky=W)

    checkbtn = Button(logframe, text="Register", fg="#fff", bg="#090", command=Chek)

    checkbtn.grid(row=3,column=2, sticky=E)

    logframe.pack()







def Exit():

    window.quit()

    exit()







def Getbook():

    closer()

    def Chek():

        firstnaem = firstnaem.rstrip("\n")

        lastnaem = books.readline().rstrip("\n")

        books  = [[firstnaem,lastnaem]]

        for i in books.readline():

            print(i[0])

            

    extframe = Frame(window, height=400, width=600)



    bname=StringVar()

    leftframe=LabelFrame(extframe, height=400, width=300)

    Label(leftframe, text="Get",font="25").place(y=0,x=0)

    Label(leftframe, text="Book ID").place(y=30,x=10)

    nr=Entry(leftframe, width=45).place(y=50,x=10)

    Label(leftframe, text="Book Name").place(y=70,x=10)

    Entry(leftframe, textvariable=bname, width=45,state="readonly",).place(y=90,x=10)

    Button(leftframe, text="Get", width=38, height=10, bg="yellow", command=Chek).place(y=121,x=9)







    leftframe.place(y=0,x=1)



    rightframe=LabelFrame(extframe, height=400, width=300)

    Label(rightframe, text="Give",font="25").place(y=0,x=0)

    

    rightframe.place(y=0,x=300)

    extframe.place(y=0,x=0)







##3

#### Start

##3



frame = Frame(window, height=400, width=600)

welcom=Label(frame,text="Welcome to Book Extange v1",font="50")

welcom.grid(row=0,column=2)

button1 = Button(frame,text="Login",font="25",width="10",height="3",bg="#00f",fg="white",command=Login)

button1.grid(row=1,column=2, sticky=W)

button2 = Button(frame,text="Register",font="25",width="10",height="3",bg="#090",fg="white",command=Register)

button2.grid(row=1,column=2, sticky=E)

frame.pack()







closebtn = Button(window, text="Close", bg="red", command=Exit)

closebtn.place(x=560)



##3

#### DROP DOWN

##3



menubar = Menu(window)

navmenu= Menu(menubar, tearoff=0)

navmenu.add_command(label="Exit",command=Exit)

navmenu.add_command(label="Home")

navmenu.add_command(label="Login", command=Login)

navmenu.add_command(label="Register", command=Register)

navmenu.add_separator()

menubar.add_cascade(label="Menu", menu=navmenu)

navmenu.add_command(label="Extange",command=Getbook)





window.config(menu=menubar)

window.mainloop()

CodePudding user response:

You need to add break after the line tkMessageBox.askokcancel("System","logged",). Also the else block should be in same indentation of the for line.

Below is the modified Chek():

    def Chek():
        for line in open("users.txt", "r").readlines():
            loginn_info = line.split()
            if name.get() == loginn_info[1] and passwd.get() == loginn_info[2]:
                tkMessageBox.askokcancel("System", "logged")
                break  # break out the loop    
        else:
            # credential not found
            tkMessageBox.askokcancel("System", "Error")
  • Related