2017-12-12 27 views
-2

最近、私はログインシステムを設計するプロジェクトを持っています。私が使用しなければならないコンテナはcsvです。私の問題は、ユーザーが新しいアカウントにサインアップするときに、行ごとにユーザー情報を保存できないことです。新しいユーザーが新しいアカウントにサインアップすると、古いアカウントの情報がカバーされ、削除されます。csvの行ごとにデータを保存する方法は?

はここでそれは私が最近、データを保存する方法で、クラスNew_Toplevel_2で私のコード→

#! /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() 

メイン節約ブロックがデフ再され(自己)です。それでも動作しますが、うまくいきません。

誰でも私にこの問題の解決を手伝ってもらえますか?

+0

'f = open( 'example.csv'、 'a') ' –

+0

うわー、うまく動く! 私はその単純な問題を知らなかった ありがとう! –

答えて

1

はわからないが、私はあなたが行を変更すべきだと思う:

f = open('example.csv', 'w') 

f = open('example.csv', 'a') 

wは、たびに、既存のデータは削除されますので、開いているファイルを切り捨てます。 aは、ファイルの末尾に改行を追加して挿入することを意味します。

+0

ありがとう!私はそのような単純な問題を認識していませんでした。 –

+0

回答を改善してくれてありがとう。 @ steven-almeroth – tekintosh

+0

確かに、@ TimBは実際に書式設定を行った –

関連する問題