我对在 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()
我做错了什么不允许我检查用户名是否正确?
原文由 Tom 发布,翻译遵循 CC BY-SA 4.0 许可协议
您需要
get
条目小部件中的值:您还应该导入您需要的内容并在变量名称中使用下划线:
你也永远不会在打开文件后关闭你的文件,所以你很有可能会遇到你意想不到的行为,在你完成文件后关闭你的文件,或者更好地再次使用
with
打开它们。