Home > Mobile >  my for loop doesn't work when i tried to make a user interface
my for loop doesn't work when i tried to make a user interface

Time:11-12

I'm making a user interface but my for loop is not working. i wanted to say somthing when th username was alredy used butmy code yust skips the for loop and then append the username and password to the list. in the txt file stand in each line username;password


import tkinter as tk
from tkinter import *

root = Tk()

root.title("WRTS oefentoets")
root.geometry("735x355")

def sign():
  userfile = open("users.txt", "a ")
  users = list(userfile.readlines())
  
  found = 0
  for line in users:
    parts = line.split(";")
    
  
    if parts[0] == username.get():
      found = 1
      break;
      
  if found == 1:
    bestaatal.place(x= 300, y= 200)
  
  else:
    with open('users.txt', "a ") as file_object:
        # Move read cursor to the start of file.
        file_object.seek(0)
        # If file is not empty then append '\n'
        data = file_object.read()
        if len(data) > 0:
            file_object.write("\n")
        # Append text at the end of file
        file_object.write(username.get()   ";"   password.get())

username = Entry(root, bg = 'black', fg = 'magenta')
password = Entry(root, bg = 'black', fg = 'magenta')
signin = Button(root, bg = 'black', fg = 'magenta', text = 'sign up', command = sign, width = 7)
signin.place(x= 343, y= 181)
username.place(x=262.5, y= 135)
password.place(x=262.5, y= 157.5)

CodePudding user response:

.readlines on a file in append mode starts reading from the end of that file

So, since from userfile you're only reading, just open it in read mode:

with open("users.txt", 'r') as userfile:
    ...  # your code
  • Related