我是python的新手,我正在尝试创建一个按钮函数,该函数可以将新数据写入文件,并且如果写入的数据匹配,则从配置文件中读取加载数据。
我面临的问题在这里:
第12行:如果list_of_files中有名字信息:
代码没有运行循环的if语句-也许我与文件匹配的变量出错了?
代码:
from tkinter import *
import tkinter.messagebox
import os
def register_user():
firstname_info = firstname.get()
lastname_info = lastname.get()
iden_info = iden.get()
email_info = email.get()
list_of_files = os.listdir()
if firstname_info in list_of_files:
file1 = open(firstname_info, "r")
verify = file1.read().splitlines()
if lastname_info in verify:
if iden_info in verify:
if email_info in verify:
print("it worked")
else:
print("user not found")
def main_screen():
global screen
screen = Tk()
global firstname
global lastname
global iden
global email
firstname = StringVar()
lastname = StringVar()
iden = StringVar()
email = StringVar()
header = Label(text = "Header")
firstname_label = Label(text = "Firstname")
lastname_label = Label(text = "lastname")
iden_label = Label(text = "Student ID")
email_label = Label(text = "Student Email")
header.grid(row=0, column=1)
firstname_label.grid(row=1, column=0)
lastname_label.grid(row=2, column=0)
iden_label.grid(row=3, column=0)
email_label.grid(row=4, column=0)
b1 = Button(text = "Submit", command = register_user)
b1.grid(row = 5, column = 1)
global firstname_entry
global lastname_entry
global iden_entry
global email_entry
firstname_entry = Entry(textvariable = firstname)
firstname_entry.grid(row = 1, column = 1)
lastname_entry = Entry(textvariable = lastname)
lastname_entry.grid(row = 2, column = 1)
iden_entry = Entry(textvariable = iden)
iden_entry.grid(row = 3, column = 1)
email_entry = Entry(textvariable = email)
email_entry.grid(row = 4, column = 1)
screen.mainloop()
main_screen()
谢谢
答案 0 :(得分:0)
如果您在stored data
的同一目录中运行脚本,则该脚本应该像os.listdir()
那样在当前目录.
的目录中搜索文件。但是,最好在stored data
中指定os.listdir()
的目录,例如os.listdir('/path/to/stored/data')
。另外,我认为下面的代码块:
if firstname_info in list_of_files:
file1 = open(firstname_info, "r")
verify = file1.read().splitlines()
if lastname_info in verify:
if iden_info in verify:
if email_info in verify:
print("it worked")
else:
print("user not found")
应更改为:
if firstname_info in list_of_files:
print('user profile found')
with open(firstname_info, "r") as file1:
verify = file1.read().splitlines()
if lastname_info in verify and iden_info in verify and email_info in verify:
print("it worked")
else:
print("user info incorrect")
else:
print("user profile not found")
答案 1 :(得分:0)
您没有将数据从GUI写入任何文件。 从文件 firstname_info 中读取之前,应先创建它。
您可以按以下代码修改 register_user()函数
def register_user():
firstname_info = firstname.get()
lastname_info = lastname.get()
iden_info = iden.get()
email_info = email.get()
list_of_files = os.listdir()
if firstname_info in list_of_files:
file1 = open(firstname_info, "r")
verify = file1.read().splitlines()
if lastname_info in verify:
if iden_info in verify:
if email_info in verify:
print("it worked")
else:
print("user not found")
else:
file1 = open(firstname_info, "w")
file1.write(lastname_info+"\n"+iden_info+"\n"+email_info)
file1.close()