最近我有一个设计登录系统的项目,我必须使用的保存容器是csv。我的问题是,当用户注册新帐户时,我无法逐行保存用户信息。当新用户注册新帐户时,将覆盖并删除旧帐户的信息。
这是我的代码→
#! /usr/bin/env python
#
# GUI module generated by PAGE version 4.9
# In conjunction with Tcl version 8.6
# Dec 02, 2017 03:27:21 PM
import sys
import csv
import tkMessageBox
try:
from Tkinter import *
except ImportError:
from tkinter import *
try:
import ttk
py3 = 0
except ImportError:
import tkinter.ttk as ttk
py3 = 1
def vp_start_gui():
global val, w, root
root = Tk()
top = New_Toplevel_1 (root)
root.mainloop()
w = None
def create_New_Toplevel_1(root, *args, **kwargs):
global w, w_win, rt
rt = root
w = Toplevel (root)
top = New_Toplevel_1 (w)
return (w, top)
def destroy_New_Toplevel_1():
global w
w.destroy()
w = None
class New_Toplevel_1:
def __init__(self, top=None):
'''This class configures and populates the toplevel window.
top is the toplevel containing window.'''
_bgcolor = '#d9d9d9' # X11 color: 'gray85'
_fgcolor = '#000000' # X11 color: 'black'
_compcolor = '#d9d9d9' # X11 color: 'gray85'
_ana1color = '#d9d9d9' # X11 color: 'gray85'
_ana2color = '#d9d9d9' # X11 color: 'gray85'
self.style = ttk.Style()
if sys.platform == "win32":
self.style.theme_use('winnative')
self.style.configure('.',background=_bgcolor)
self.style.configure('.',foreground=_fgcolor)
self.style.configure('.',font="TkDefaultFont")
self.style.map('.',background=
[('selected', _compcolor), ('active',_ana2color)])
top.geometry("402x399+496+146")
top.title("Login Page")
top.configure(background="#d9d9d9")
self.Label1 = Label(top)
self.Label1.place(relx=0.17, rely=0.15, height=22, width=41)
self.Label1.configure(background="#d9d9d9")
self.Label1.configure(disabledforeground="#a3a3a3")
self.Label1.configure(foreground="#000000")
self.Label1.configure(text='''Name''')
self.Label2 = Label(top)
self.Label2.place(relx=0.17, rely=0.25, height=22, width=18)
self.Label2.configure(background="#d9d9d9")
self.Label2.configure(disabledforeground="#a3a3a3")
self.Label2.configure(foreground="#000000")
self.Label2.configure(text='''ID''')
self.TEntry1 = ttk.Entry(top)
self.TEntry1.place(relx=0.4, rely=0.15, relheight=0.06, relwidth=0.36)
self.TEntry1.configure(takefocus="")
self.TEntry1.configure(cursor="ibeam")
self.TEntry2 = ttk.Entry(top)
self.TEntry2.place(relx=0.4, rely=0.25, relheight=0.06, relwidth=0.36)
self.TEntry2.configure(takefocus="")
self.TEntry2.configure(cursor="ibeam")
self.Label3 = Label(top)
self.Label3.place(relx=0.17, rely=0.35, height=22, width=57)
self.Label3.configure(background="#d9d9d9")
self.Label3.configure(disabledforeground="#a3a3a3")
self.Label3.configure(foreground="#000000")
self.Label3.configure(text='''Password''')
self.Label3.configure(width=57)
self.TEntry3 = ttk.Entry(top,show="*")
self.TEntry3.place(relx=0.4, rely=0.35, relheight=0.06, relwidth=0.36)
self.TEntry3.configure(takefocus="")
self.TEntry3.configure(cursor="ibeam")
self.Button1 = Button(top,command=self.regist)
self.Button1.place(relx=0.17, rely=0.64, height=46, width=88)
self.Button1.configure(activebackground="#d9d9d9")
self.Button1.configure(activeforeground="#000000")
self.Button1.configure(background="#d9d9d9")
self.Button1.configure(disabledforeground="#a3a3a3")
self.Button1.configure(foreground="#000000")
self.Button1.configure(highlightbackground="#d9d9d9")
self.Button1.configure(highlightcolor="black")
self.Button1.configure(pady="0")
self.Button1.configure(text='''Sign up''')
self.Button1.configure(width=88)
self.Button2 = Button(top,command=self.check)
self.Button2.place(relx=0.62, rely=0.64, height=46, width=88)
self.Button2.configure(activebackground="#d9d9d9")
self.Button2.configure(activeforeground="#000000")
self.Button2.configure(background="#d9d9d9")
self.Button2.configure(disabledforeground="#a3a3a3")
self.Button2.configure(foreground="#000000")
self.Button2.configure(highlightbackground="#d9d9d9")
self.Button2.configure(highlightcolor="black")
self.Button2.configure(pady="0")
self.Button2.configure(text='''Login''')
self.Button2.configure(width=88)
def check(self):
import csv
f=open("example.csv","r")
q=csv.reader(f)
l=list(q)
#print l
if len(self.TEntry1.get())==0 and len(self.TEntry2.get())==0 and len(self.TEntry3.get())==0:
tkMessageBox.showinfo("Error","somewhere is empty")
if (self.TEntry1.get()==l[0][0] and self.TEntry2.get()==l[0][2] and self.TEntry3.get()==l[0][4]) and (len(self.TEntry1.get())!=0 and len(self.TEntry2.get())!=0 and len(self.TEntry3.get())!=0):
tkMessageBox.showinfo("Success", "Hi!! "+str(self.TEntry1.get()))
elif len(self.TEntry1.get())!=0 and len(self.TEntry2.get())!=0 and len(self.TEntry3.get())!=0:
tkMessageBox.showinfo("Fail", "Sorry!! You are not " + str(self.TEntry1.get()))
def regist(self):
root.destroy()
#root.attributes('-fullscreen', True)
#New_Toplevel_2()
vp_start_gui2()
def vp_start_gui2():
global val2, w2, root2
root2 = Tk()
top2 = New_Toplevel_2 (root2)
root2.mainloop()
w2 = None
def create_New_Toplevel_2(root2, *args, **kwargs):
global w2, w_win2, rt2
rt2 = root2
w2 = Toplevel (root2)
top2 = New_Toplevel_2 (w2)
return (w2, top2)
def destroy_New_Toplevel_2():
global w2
w2.destroy()
w2 = None
class New_Toplevel_2:
def __init__(self, top=None):
'''This class configures and populates the toplevel window.
top is the toplevel containing window.'''
_bgcolor = '#d9d9d9' # X11 color: 'gray85'
_fgcolor = '#000000' # X11 color: 'black'
_compcolor = '#d9d9d9' # X11 color: 'gray85'
_ana1color = '#d9d9d9' # X11 color: 'gray85'
_ana2color = '#d9d9d9' # X11 color: 'gray85'
self.style = ttk.Style()
if sys.platform == "win32":
self.style.theme_use('winnative')
self.style.configure('.',background=_bgcolor)
self.style.configure('.',foreground=_fgcolor)
self.style.configure('.',font="TkDefaultFont")
self.style.map('.',background=
[('selected', _compcolor), ('active',_ana2color)])
top.geometry("445x524+471+129")
top.title("Sing up")
top.configure(background="#d9d9d9")
self.Label1 = Label(top)
self.Label1.place(relx=0.2, rely=0.15, height=22, width=41)
self.Label1.configure(background="#d9d9d9")
self.Label1.configure(disabledforeground="#a3a3a3")
self.Label1.configure(foreground="#000000")
self.Label1.configure(text='''Name''')
self.Label2 = Label(top)
self.Label2.place(relx=0.2, rely=0.23, height=22, width=37)
self.Label2.configure(background="#d9d9d9")
self.Label2.configure(disabledforeground="#a3a3a3")
self.Label2.configure(foreground="#000000")
self.Label2.configure(text='''Email''')
self.Label3 = Label(top)
self.Label3.place(relx=0.19, rely=0.31, height=22, width=48)
self.Label3.configure(background="#d9d9d9")
self.Label3.configure(disabledforeground="#a3a3a3")
self.Label3.configure(foreground="#000000")
self.Label3.configure(text='''Gender''')
self.Label4 = Label(top)
self.Label4.place(relx=0.18, rely=0.38, height=22, width=67)
self.Label4.configure(background="#d9d9d9")
self.Label4.configure(disabledforeground="#a3a3a3")
self.Label4.configure(foreground="#000000")
self.Label4.configure(text='''WatchID''')
self.Label4.configure(width=67)
self.Radiobutton1 = Radiobutton(top)
self.Radiobutton1.place(relx=0.43, rely=0.31, relheight=0.05
, relwidth=0.14)
self.Radiobutton1.configure(activebackground="#d9d9d9")
self.Radiobutton1.configure(activeforeground="#000000")
self.Radiobutton1.configure(background="#d9d9d9")
self.Radiobutton1.configure(disabledforeground="#a3a3a3")
self.Radiobutton1.configure(foreground="#000000")
self.Radiobutton1.configure(highlightbackground="#d9d9d9")
self.Radiobutton1.configure(highlightcolor="black")
self.Radiobutton1.configure(justify=LEFT)
self.Radiobutton1.configure(text='''Male''')
self.Radiobutton2 = Radiobutton(top)
self.Radiobutton2.place(relx=0.62, rely=0.31, relheight=0.05
, relwidth=0.14)
self.Radiobutton2.configure(activebackground="#d9d9d9")
self.Radiobutton2.configure(activeforeground="#000000")
self.Radiobutton2.configure(background="#d9d9d9")
self.Radiobutton2.configure(disabledforeground="#a3a3a3")
self.Radiobutton2.configure(foreground="#000000")
self.Radiobutton2.configure(highlightbackground="#d9d9d9")
self.Radiobutton2.configure(highlightcolor="black")
self.Radiobutton2.configure(justify=LEFT)
self.Radiobutton2.configure(text='''Female''')
self.Button1 = Button(top,command=self.re)
self.Button1.place(relx=0.56, rely=0.68, height=46, width=88)
self.Button1.configure(activebackground="#d9d9d9")
self.Button1.configure(activeforeground="#000000")
self.Button1.configure(background="#d9d9d9")
self.Button1.configure(disabledforeground="#a3a3a3")
self.Button1.configure(foreground="#000000")
self.Button1.configure(highlightbackground="#d9d9d9")
self.Button1.configure(highlightcolor="black")
self.Button1.configure(pady="0")
self.Button1.configure(text='''Register''')
self.Button1.configure(width=88)
self.Label5 = Label(top)
self.Label5.place(relx=0.21, rely=0.46, height=22, width=37)
self.Label5.configure(background="#d9d9d9")
self.Label5.configure(disabledforeground="#a3a3a3")
self.Label5.configure(foreground="#000000")
self.Label5.configure(text='''Age''')
self.TEntry1 = ttk.Entry(top)
self.TEntry1.place(relx=0.42, rely=0.15, relheight=0.04, relwidth=0.33)
self.TEntry1.configure(takefocus="")
self.TEntry1.configure(cursor="ibeam")
self.TEntry2 = ttk.Entry(top)
self.TEntry2.place(relx=0.42, rely=0.23, relheight=0.04, relwidth=0.33)
self.TEntry2.configure(takefocus="")
self.TEntry2.configure(cursor="ibeam")
self.TEntry3 = ttk.Entry(top)
self.TEntry3.place(relx=0.42, rely=0.38, relheight=0.04, relwidth=0.33)
self.TEntry3.configure(takefocus="")
self.TEntry3.configure(cursor="ibeam")
self.TEntry4 = ttk.Entry(top)
self.TEntry4.place(relx=0.42, rely=0.46, relheight=0.04, relwidth=0.33)
self.TEntry4.configure(takefocus="")
self.TEntry4.configure(cursor="ibeam")
self.Label6 = Label(top)
self.Label6.place(relx=0.19, rely=0.54, height=22, width=57)
self.Label6.configure(background="#d9d9d9")
self.Label6.configure(disabledforeground="#a3a3a3")
self.Label6.configure(foreground="#000000")
self.Label6.configure(text='''Password''')
self.Label6.configure(width=57)
self.TEntry5 = ttk.Entry(top,show="*")
self.TEntry5.place(relx=0.42, rely=0.55, relheight=0.04, relwidth=0.33)
self.TEntry5.configure(takefocus="")
self.TEntry5.configure(cursor="ibeam")
def re(self):
import csv
d1 = str(self.TEntry1.get())
d2 = str(self.TEntry2.get())
d3 = str(self.TEntry3.get())
d4 = str(self.TEntry4.get())
d5 = str(self.TEntry5.get())
dict={"Name":str(self.TEntry1.get()),"Email":str(self.TEntry2.get()),"WatchID":str(self.TEntry3.get()),"Age":str(self.TEntry4.get()),"Password":str(self.TEntry5.get())}
info=[dict["Name"],dict["Email"],dict["WatchID"],dict["Age"],dict["Password"]]
f = open('example.csv', 'w')
w = csv.writer(f)
w.writerow(info)
f.flush()
f.close()
root2.destroy()
vp_start_gui()
if __name__ == '__main__':
#App=New_Toplevel_1()
vp_start_gui()
#vp_start_gui2()
主要的保存块是New_Toplevel_2类中的def(self),这就是我最近保存数据的方式。它仍然有效,但不是很好。
有人可以帮我解决这个问题吗?
答案 0 :(得分:1)
我认为你应该更改这一行:
f = open('example.csv', 'w')
到
f = open('example.csv', 'a')
w
在文件打开时截断文件,因此每次都会删除现有数据。 a
表示追加并在文件末尾添加新行。