【问题标题】:Tkinter Password SystemTkinter 密码系统
【发布时间】:2015-06-14 11:06:36
【问题描述】:

我对在 python 中使用 Tkinter 还很陌生,我想知道是否有人可以帮助我使用我的密码系统。在检查用户输入的用户名和实际用户名时我很挣扎,此时我并不太担心密码部分,因为一旦我让用户名部分工作,它应该很容易,只需稍微调整一下来自用户名部分的代码。无论如何,这是我的代码:

#Toms Password system

import tkinter
from tkinter import *
from tkinter import ttk 

username = ("Tom")
password = ("") 
usernameguess1 = ("")
passwordguess1 = ("")


#ignore the file writing part,I'll sort this out later.

file = open("userlogondata.txt", "w")
file.write("User Data:\n")

file = open("userlogondata.txt", "r")

def trylogin():
   print ("Trying to login...")
   if usernameguess == username:
       print ("Complete sucsessfull!")
       messagebox.showinfo("-- COMPLETE --", "You Have Now Logged In.",icon="info")
   else:
       print ("Error: (Incorrect value entered)")
       messagebox.showinfo("-- ERROR --", "Please enter valid infomation!", icon="warning")



#Gui Things
window = tkinter.Tk() 
window.resizable(width=FALSE, height=FALSE)
window.title("Log-In")
window.geometry("200x150")
window.wm_iconbitmap("applicationlogo.ico")


#Creating the username & password entry boxes
usernametext = tkinter.Label(window, text="Username:")
usernameguess = tkinter.Entry(window)
passwordtext = tkinter.Label(window, text="Password:")
passwordguess = tkinter.Entry(window, show="*")

usernameguess1 = usernameguess




#attempt to login button
attemptlogin = tkinter.Button(text="Login", command=trylogin)



usernametext.pack()
usernameguess.pack()
passwordtext.pack()
passwordguess.pack()
attemptlogin.pack()
#Main Starter
window.mainloop()

我做错了什么,无法检查用户名是否正确?

【问题讨论】:

  • 这太笼统了,请尝试提出具体问题,以便我们更好地帮助您。另外我认为“if usernameguess==username:”之后有缩进问题,下面的打印应该缩进。否则“完成成功!”将始终打印。而且我认为您的意思是“成功”而不是“成功”。
  • 基本上我想做的就是用实际数据检查用户输入的数据,所以我想检查“usernameguess”是否等于“username”,如果是的话显示一条类似“正确”的消息

标签: python user-interface python-3.x tkinter


【解决方案1】:

您需要get Entry 小部件中的值:

 if usernameguess.get() == username:

您还应该导入您需要的内容并在变量名中使用下划线:

from tkinter import messagebox, Label, Button, FALSE, Tk, Entry

username = ("Tom")
password = ("")


def try_login():
    print("Trying to login...")
    if username_guess.get() == username:
        messagebox.showinfo("-- COMPLETE --", "You Have Now Logged In.", icon="info")
    else:
        messagebox.showinfo("-- ERROR --", "Please enter valid infomation!", icon="warning")

#Gui Things
window = Tk()
window.resizable(width=FALSE, height=FALSE)
window.title("Log-In")
window.geometry("200x150")

#Creating the username & password entry boxes
username_text = Label(window, text="Username:")
username_guess = Entry(window)
password_text = Label(window, text="Password:")
password_guess = Entry(window, show="*")

#attempt to login button
attempt_login = Button(text="Login", command=try_login)

username_text.pack()
username_guess.pack()
password_text.pack()
password_guess.pack()
attempt_login.pack()
#Main Starter
window.mainloop()

您也永远不会在打开文件后关闭文件,因此您很有可能会遇到意想不到的行为,请在完成文件后关闭文件,或者最好再次使用with 打开它们。

【讨论】:

  • 您可以对密码进行哈希处理,然后将其与用户输入进行比较
【解决方案2】:

下面是一个简单的检查示例:

from tkinter import *

root = Tk()

username = "abc" #that's the given username
password = "cba" #that's the given password

#username entry
username_entry = Entry(root)
username_entry.pack()

#password entry
password_entry = Entry(root, show='*')
password_entry.pack()

def trylogin(): #this method is called when the button is pressed
    #to get what's written inside the entries, I use get()
    #check if both username and password in the entries are same of the given ones
    if username == username_entry.get() and password == password_entry.get():
        print("Correct")
    else:
        print("Wrong")

#when you press this button, trylogin is called
button = Button(root, text="check", command = trylogin) 
button.pack()

#App starter
root.mainloop()

看看这个小预览:

About entryAbout button.


以这种方式执行此代码不是一个好习惯。推荐使用 OOP。但我只是想向您展示资源。

【讨论】:

    【解决方案3】:
    import tkMessageBox
    from Tkinter import *
    def show_entry_fields():
        # print("Nombre : %s\nApellido: %s" %(e1.get(), e2.get()))
        if e1.get() == 'ivan' and e2.get() == '123456':
            tkMessageBox.showinfo(message="Bienvenido Ivan")
        else:
            print "Usuario Incorrecto..."
    
    master = Tk()
    Label(master, text="Nombre").grid(row=0)
    Label(master, text="Apellido").grid(row=1)
    
    e1 = Entry(master)
    e2 = Entry(master, show="*")
    e1.insert(10, "Jonas")
    e2.insert(10, "reyes")
    
    e1.grid(row=0, column=1)
    e2.grid(row=1, column=1)
    
    Button(master, text='Quit', command=master.quit).grid(row=3, column=0, sticky=W, pady=4)
    Button(master, text='Show', command=show_entry_fields).grid(row=3, column=1, sticky=W, pady=4)
    mainloop()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-03
      • 2017-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多