2017-12-12 1 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는 실제로 서식을 지정했습니다. –

관련 문제