2017-09-11 1 views
1

메모장에서 작업 시스템의 데이터가 들어있는 데이터베이스로 tkinter에 암호 시스템을 만들려고하지만 입력 필드에 데이터를 삽입 할 때 오류가 발생합니다. 실패했습니다 .txt 파일을 만들었지 만 파일에서 파일을 읽을 수 없습니다.이 작업을 수행하는 방법에 대한 제안. 파일에서 데이터를 읽는 tkinter에서 암호 시스템을 만드는 방법

import tkinter as tk 
import sys 
from tkinter import messagebox 


now = open("passdoc.txt","w+") 
now.write("user\n") 
now.write("python3") 
now.close() 

def login_in(): 
    with open("passdoc.txt") as f: 
     new = f.readlines() 
     name = new[0].rstrip() 
     password = new[1].rstrip() 
    if entry1.get() == new[0] in passdoc.txt and entry2.get() == new[1] in 
passdoc.txt: 
     root.deiconify() 
     log.destroy() 
    else: 
     messagebox.showerror("error","login Failed") 


def close(): 
    log.destroy() #Removes toplevel window 
    root.destroy() #Removes root window 
    sys.exit() #Ends the script 


root=tk.Tk() 
log = tk.Toplevel() # 

root.geometry("350x350") 
log.geometry("200x200") 

entry1 = tk.Entry(log) #Username entry 
entry2 = tk.Entry(log) #Password entry 
button1 = tk.Button(log, text="Login", command=login_in) #Login button 
button2 = tk.Button(log, text="Cancel", command=close) #Cancel button 
label1 = tk.Label(root, text="tkinter password system") 

entry1.pack() 
entry2.pack() 
button1.pack() 
button2.pack() 
label1.place(x=30,y=300) 


label = tk.Label(root, text="welcome").pack() 

root.withdraw() 
root.mainloop() 

은 나도이 기능을 만들었지 만 모두 당신은 몇 가지 작업을 필요로하지만 주요 문제는 당신 login_in() 기능에 코드에서 몇 가지를 가지고 나

def login_in(): 
    with open("passdoc.txt") as f: 
     new = f.readlines() 
     name = new[0].rstrip() 
     password = new[1].rstrip() 
    if entry1.get() == name in passdoc.txt and entry2.get() == password in 
passdoc.txt: 
     root.deiconify() 
     log.destroy() 
    else: 
     messagebox.showerror("errror","login failed") #error login failed 
(corrections) 
+0

버튼을 눌렀을 때 이름과 비밀번호의 내용을 확인하려고 했습니까? 이 두 줄 바로 아래에 print 문을 추가하여 확인할 수 있습니다. – Lafexlos

+0

또한 if 문은 잘못되었습니다. 이 코드에서 NameError를 해당 줄에 가져와야합니다. – Lafexlos

답변

0

위해 밖으로 작동하지 않을 것 같다.

if 문구가 잘못되었습니다. 왜 그렇게 쓰는지 모르겠지만 해결할 수 있습니다. 당신이 name = new[0].rstrip()password = new[1].rstrip() 을 정의하기 때문에

당신은 사용자가 올바른 자격 증명을 입력 할 경우 확인 namepassword를 사용할 수 있습니다.

그래서 if 문은 다음과 같아야합니다

in passdoc.txt의 사용은 아무것도하지 않고 파이썬 passdoc.txt가 정의되지 않은 변수처럼 보이기 때문에 작동하지 않을 수 있고, if 문에 실패
if entry1.get() == name and entry2.get() == password: 

. 당신이 그렇게라는 이름의 변수를 만들지 않은 fpassdoc.txt의 모든 내용을 가져 기억 passdoc 당신은 당신이 namepassword 변수를 제거 할 수 코드 약간을 단축하고 싶었다면 하나는 with open()

에 대한 f라는 이름의 생성

def login_in(): 
    with open("passdoc.txt") as f: 
     new = f.readlines() 
     name = new[0].rstrip() 
     password = new[1].rstrip() 
    if entry1.get() == name and entry2.get() == password: 
     root.deiconify() 
     log.destroy() 
    else: 
     messagebox.showerror("error","login Failed") 

을 또는이 : 그냥 그래서 login_in() 기능은 하나 같이 수 new

와 if 문을 쓰기 :

def login_in(): 
    with open("passdoc.txt") as f: 
     new = f.readlines() 
    if entry1.get() == new[0].rstrip() and entry2.get() == new[1].rstrip(): 
     root.deiconify() 
     log.destroy() 
    else: 
     messagebox.showerror("error","login Failed") 
+0

수정 해 주셔서 감사합니다. 앞으로의 안내가 될 것입니다. –

관련 문제