我正在尝试创建一个Tkinter登录Gui但是如果我单击登录它会显示消息“TypeError:描述符'readlines'的'文件'对象需要参数”我尝试寻找答案,其中大多数是因为区分大小写的错误。请有人帮帮我。我已经正确设置了profile.txt文件(我100%肯定)
def LogIn():
name=input("Please enter your name: ")
file = open(name.lower() + "profile.txt", "r")
import Tkinter
import time
window = Tkinter.Tk()
window.title("Python Games Login")
window.geometry("270x210")
window.configure(bg="#39d972")
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.")
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")
user = Tkinter.Entry(window)
passw = Tkinter.Entry(window, show='*')
go = Tkinter.Button(window, text="Log in!", command = callback, bg="#93ff00")
title1.pack()
usertitle.pack()
user.pack()
passtitle.pack()
passw.pack()
go.pack()
message.pack()
window.mainloop()
答案 0 :(得分:2)
你的问题是你在函数中分配给file
并且它没有传播到全局变量,因此在函数callback
中它不是你所期望的。
file
它(在评论中暗示)内置python类型,因此您会收到此类错误,而不是NameError
,因为它将是未定义的变量。
请检查示例this question以更好地了解变量作用域在python中的工作方式。