简体   繁体   English

Python GUI登录程序

[英]Python GUI login program

Me and my friend are making a program that requires a login at the start. 我和我的朋友正在制作一个需要在开始时登录的程序。 We've managed to get the user to input details, and have the program create a text file called (users name) profile with the details in. each one is formatted like this: 我们设法让用户输入详细信息,并让程序创建一个名为(用户名)配置文件的文本文件,其中包含详细信息。每个文件的格式如下:

(Name) (名称)

(Username) (用户名)

(Password) (密码)

when you want to log in, the program asks for your name and finds the file with the name (whatever they typed) profile. 当您想要登录时,程序会询问您的姓名并找到具有名称(无论他们输入什么)的文件。 if it exists, the program then opens a GUI window and asks for the username and password, and if you put in the correct details for the file it has opened, it says that the details are wrong. 如果它存在,程序然后打开一个GUI窗口并询问用户名和密码,如果你输入它打开的文件的正确详细信息,它说细节是错误的。 We think its to do with the variables, but we've tried lot of different ways of laying it out etc and can't find the problem. 我们认为它与变量有关,但是我们已经尝试了很多不同的方法来解决它们并且找不到问题。 Can anyone help? 有人可以帮忙吗? (The code I have included is only the GUI part including the bit that isn't working, the rest is fine. (我所包含的代码只是GUI部分,包括不工作的位,其余的都没问题。

# Log in
def LogIn():
    name=input("Please enter your name: ")
    file = open(name.lower() + " profile.txt", "r")
#+=========GUI===========GUI============GUI===========+

    #mport modules
    import tkinter
    import time

    #---Window---#
    #make window
    window = tkinter.Tk()
    #change title
    window.title("Python Games Login")
    #change size
    window.geometry("270x210")
    #change window icon
    window.wm_iconbitmap("Login icon.ico")
    #change window colour
    window.configure(bg="#39d972")

    #---Commands---#
    #go
    def callback():
        line = file.readlines()
        username = user.get()
        password = passw.get()
        if username == line[1] and password == line[2]:
            message.configure(text = "Logged in.")
        else:
            message.configure(text = "Username and password don't match the account \n under the name;\n \'" + name + "\'. \nPlease try again.")
    #---Widgets---#
    #labels
    title1 = tkinter.Label(window, text="--Log in to play the Python Games--\n", bg="#39d972")
    usertitle = tkinter.Label(window, text="---Username---", bg="#39d972")
    passtitle = tkinter.Label(window, text="---Password---", bg="#39d972")
    message = tkinter.Label(window, bg="#39d972")

    #text entry windows
    user = tkinter.Entry(window)
    passw = tkinter.Entry(window, show='*')

    #buttons
    go = tkinter.Button(window, text="Log in!", command = callback, bg="#93ff00")

    #pack widgets
    title1.pack()
    usertitle.pack()
    user.pack()
    passtitle.pack()
    passw.pack()
    go.pack()
    message.pack()

    #start window
    window.mainloop()

#+===================GUI END=====================+

I would use python's pickle module to save data. 我会使用python的pickle模块来保存数据。 It is much more high level than just saving it in a text file. 它比将其保存在文本文件中要高得多。 In my example, I pickled a list of dictionaries. 在我的例子中,我腌制了一个词典列表。

import pickle
def LogIn():
    name=input("Please enter your name: ").lower()
    #data.pickle should be a list of dictionaries representing a user
    usernames= pickle.load('data.pickle')
    for userdata in usernames:
        if userdata['name']== name:
            return userdata
    #didn't find the name
    print('could not find '+ name+ ' in data.pickle')
    return None

A note about pickle from the docs : 关于文档中的 pickle的注释:

Warning: 警告:

The pickle module is not intended to be secure against erroneous or maliciously constructed data. pickle模块不是为了防止错误或恶意构造的数据。 Never unpickle data received from an untrusted or unauthenticated source. 切勿取消从不受信任或未经身份验证的来源收到的数据。

Also check out shelve and marshal , they perform similar results, or consider saving it in a json file format (python has a json module) 还要检查shelvemarshal ,它们执行类似的结果,或考虑以json文件格式保存它(python有一个json模块)

Notice that readlines does not strip the end-of-line character(s) from the lines: 请注意, readlines不会从行中删除行尾字符:

In [57]: f = open('data','r')

In [58]: f.readlines()
Out[58]: ['index,value\n', '0,16714217840939775\n', '1,16714217840939776 \n']

So username == line[1] is probably failing since username does not include the end-of-line character. 因此, username == line[1]可能会失败,因为username不包含行尾字符。 And the same goes for password == line[2] . password == line[2]

A simple fix would be to use 一个简单的解决方法是使用

username == line[1].strip()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM