我在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()
我做错了什么,不允许我检查用户名是否正确?
答案 0 :(得分:3)
您需要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
打开它们。
答案 1 :(得分:1)
以下是一个如何检查的简单示例:
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()
看一下这个小预览:
以这种方式执行此代码并不是一个好习惯。建议使用OOP。但我只是想向您展示资源。
答案 2 :(得分:0)
import deap
deap.download()