用户名和密码必须与文本文件中的数据匹配

时间:2017-10-19 11:22:08

标签: python tkinter

我设计了一个用户名和密码程序。当用户输入用户名和密码时,应检查它是否与文本文件中的数据匹配。问题是,当我输入正确的用户名和密码时,它会显示拒绝访问权限。请帮我找到解决方案。

from tkinter import *

class Application(Frame):

    def __init__(self,master):

        Frame.__init__(self,master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):


        self.instruction = Label(self,text='Enter your Username')
        self.instruction.grid(row=0, column=0, columnspan=2,sticky=W)

        self.username=Entry(self)
        self.username.grid(row=1, column =1, sticky=W)

        self.sumbit_button=Button(self,text='Submit',command= self.reveal)
        self.sumbit_button.grid(row=5,column=0,sticky=W)

        self.instruction = Label(self,text='Enter a password')
        self.instruction.grid(row=3, column=0, columnspan=2,sticky=W)

        self.password=Entry(self)
        self.password.grid(row=4, column =1, sticky=W)

        self.text = Text(self,width=35, height=5, wrap=WORD)
        self.text.grid(row=6, column=0, columnspan=2,sticky=W)


    def reveal(self):
        file=open('data.RTF','r')
        data1=file.readlines()
        file.close

        content2=self.username.get()
        content=self.password.get()


        if content2==(data1[0]) and content==(data1[1]): 
            message='You have access to something special.'

        else:
            message='Access denied.'

        self.text.delete(0.0,END)
        self.text.insert(0.0,message)

root=Tk()
root.title('Username and Password')
root.geometry('250x150')
app=Application(root)

root.mainloop()

1 个答案:

答案 0 :(得分:0)

我已更新代码以显示在何处执行此操作,之前在该行上添加注释。

from tkinter import *

class Application(Frame):

    def __init__(self,master):

        Frame.__init__(self,master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):


        self.instruction = Label(self,text='Enter your Username')
        self.instruction.grid(row=0, column=0, columnspan=2,sticky=W)

        self.username=Entry(self)
        self.username.grid(row=1, column =1, sticky=W)

        self.sumbit_button=Button(self,text='Submit',command= self.reveal)
        self.sumbit_button.grid(row=5,column=0,sticky=W)

        self.instruction = Label(self,text='Enter a password')
        self.instruction.grid(row=3, column=0, columnspan=2,sticky=W)

        self.password=Entry(self)
        self.password.grid(row=4, column =1, sticky=W)

        self.text = Text(self,width=35, height=5, wrap=WORD)
        self.text.grid(row=6, column=0, columnspan=2,sticky=W)


    def reveal(self):
        file=open('data.RTF','r')
        data1=file.readlines()
        file.close

        content2=self.username.get()
        content=self.password.get()

        # Code updated on next line
        if content2==(data1[0].replace("\n","").replace("\r","")) and content==(data1[1].replace("\n","").replace("\r","")): 
            message='You have access to something special.'

        else:
            message='Access denied.'

        self.text.delete(0.0,END)
        self.text.insert(0.0,message)

root=Tk()
root.title('Username and Password')
root.geometry('250x150')
app=Application(root)

root.mainloop()