所以我创建了一个tkinter程序,将用户数据写入文件,但在编写变量时转为数字
这是写入文件的内容:
.15212688
.15213328
.15213232
INVALID REGISRATION NUMBER
这是我的代码:
def show_entry_fields():#creates a function called show entry fields.
print("First Name: %s\nLast Name: %s\nRegistration number:%s" % (Fname.get(), Lname.get(), reg.get()))
if re.match('^[A-Za-z]{2}[0-9]{2}[A-Za-z]{3}$', reg.get()):#validates the users reg number by ching that its in the correct format
w.configure(text='That is a valid registration number')# if the reg is valid, tells user
else:
w.configure(text='Invalid registration number')#user is told that reg is not valid
data = open("invalid.txt", "w")#users data is writen to a file called 'invalid'
data.write(str(Fname))#first name
data.write('\n')#new line
data.write(str(Lname))#last name
data.write('\n')#new line
data.write(str(reg))# the casr's regitration number
data.write('\n')#new line
data.write('INVALID REGISRATION NUMBER')# the perpiose of being writen to the file
data.close()#closes the file when finished.
如果有人能指出我正确的方向,请提前谢谢
答案 0 :(得分:1)
你应该使用
data.write(str(Fname.get()))#first name
data.write('\n')#new line
data.write(str(Lname.get()))#last name
data.write(str(reg.get()))
在您的代码中,您将对象转换为字符串。