我正在制作用戶界面,但我的 for 回圈不起作用。當用戶名已經被使用時,我想說點什么,但是我的代碼跳過了 for 回圈,然后將用戶名和密碼附加到串列中。在 txt 檔案中的每一行 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)
uj5u.com熱心網友回復:
.readlines在附加模式下的檔案上從該檔案的末尾開始讀取
所以,既然userfile你只是在閱讀,只需在閱讀模式下打開它:
with open("users.txt", 'r') as userfile:
... # your code
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/532061.html
上一篇:為所有數字列生成facet_wrap直方圖,并為“for”回圈中的另一列生成分面
下一篇:檢查陣列中是否有負數和重復數
