所以我用Python创建了我的第一个程序,并且我把它全部组成了很小的部分而且我很难将它们放在一起。
我为这篇文章提取的代码基本上都是使用预先制作的.txt登录,名为&#34; yournamehere profile.txt&#34;其中前两行有您的用户名和密码,经过检查并允许您登录。</ p>
然后我就拥有它以便您可以输入您的详细信息,但是我收到错误:TypeError:descriptor&#39; write&#39;需要一个&#39;文件&#39;对象但收到了一个&#39; str,在查找之后我假设是由于文件没有在正确的代码部分打开而引起的。
对不起,如果我的代码混乱或做得不好,这是我的第一个程序,所以如果你有任何提示或方法可以让它更好,我也会对它们表示赞赏:P < / p>
from Tkinter import *
root = Tk()
root.configure(bg="#CCFFCC")
#========--------------PERSONAL DETAILS START-------------==========#
def Personal():
Personal=Toplevel(root)
Personal.title("Personal Details")
Personal.configure(bg="#CCFFCC")
#----COMMANDS W/ WIDGETS----#
#First Name
def WriteFName():
file.write("\n")
file.write("First Name: ")
file.write(str(entryFName.get()))
file.write("\n")
labelSaveFName.configure(text="Saved")
labelFName = Label(Personal, text="First name: ", bg="#ccffcc")
entryFName = Entry(Personal)
buttonFName = Button(Personal, text="Save", command=WriteFName, bg="#93FF00")
labelSaveFName = Label(Personal, text="...", bg="#ccffcc")
#Last Name
def WriteLName():
file.write("Last Name: ")
file.write(str(entryLName.get()))
file.write("\n")
labelSaveLName.configure(text="Saved")
labelLName = Label(Personal, text="Last name: ", bg="#ccffcc")
entryLName = Entry(Personal)
buttonLName = Button(Personal, text="Save", command=WriteLName, bg="#93FF00")
labelSaveLName = Label(Personal, text="...", bg="#ccffcc")
#Age
def WriteAge():
file.write("Age: ")
file.write(str(entryAge.get()))
file.write("\n")
labelSaveAge.configure(text="Saved")
labelAge = Label(Personal, text="Age: ", bg="#ccffcc")
entryAge = Entry(Personal)
buttonAge = Button(Personal, text="Save", command=WriteAge, bg="#93FF00")
labelSaveAge = Label(Personal, text="...", bg="#ccffcc")
#----GRID----#
labelFName.grid(row=0, column=0)
entryFName.grid(row=0, column=1)
buttonFName.grid(row=0, column=2)
labelSaveFName.grid(row=0,column=3)
labelLName.grid(row=1, column=0)
entryLName.grid(row=1, column=1)
buttonLName.grid(row=1, column=2)
labelSaveLName.grid(row=1, column=3)
labelAge.grid(row=2, column=0)
entryAge.grid(row=2, column=1)
buttonAge.grid(row=2, column=2)
labelSaveAge.grid(row=2, column=3)
#=================--------PERSONAL DETAILS END-------================#
#=====----CHOOSING WHAT TO INPUT START----=====#
def details():
detailswindow=Toplevel(root)
detailswindow.configure(bg="#CCFFCC")
detailswindow.title("Choose")
def end():
detailswindow.destroy()
#---WIDGETS---#
PersonalButton = Button(detailswindow, text="Personal Details", command=Personal, bg="#93ff00")
CloseButton = Button(detailswindow, text="Close Window", command=end, bg="#93ff00")
#---GRID---#
PersonalButton.grid(row=0, column=0)
CloseButton.grid(row=1, column=1)
#=====----CHOOSING WHAT TO INPUT END----=====#
#======----LOG-IN WINDOW START----======#
def newwindow():
login=Toplevel(root)
login.configure(bg="#CCFFCC")
name=raw_input("Please enter your name: ")
file = open(name.lower() + " profile.txt", "a+")
#---Commands---#
def end():
login.destroy()
def callback():
line = file.readlines()
username = user.get()
password = passw.get()
if username == line[0].strip() and password == line[1].strip():
Message.configure(text = "Logged in.")
proceed.configure(text="Proceed", command=details)
else:
Message.configure(text = "Username and password don't match the account \n under the name;\n \'" + name + "\'. \nPlease try again.")
return f
#---LOG-IN WIDGETS---#
#labels
LogInTitle = Label(login, text="Please log in.", bg="#CCFFCC").grid(row=0, column=1)
UserTitle = Label(login, text="Username:", bg="#CCFFCC").grid(row=1, column=0)
PassTitle = Label(login, text="Password:", bg="#CCFFCC").grid(row=2, column=0)
Message = Label(login, bg="#CCFFCC")
#text entry windows
user = Entry(login)
passw = Entry(login, show='*')
#buttons
go = Button(login, text="Log in!", command = callback, bg="#93ff00").grid(row=3, column=1)
proceed = Button(login, text="...", bg="#93ff00")
close = Button(login, text="Close window", command=end, bg="#93FF00")
#===--LOG-IN GRID===--#
user.grid(row=1, column=1)
passw.grid(row=2, column=1)
Message.grid(row=4, column=1)
proceed.grid(row=5, column=1)
close.grid(row=5, column=0)
#=====----LOG-IN WINDOW END----=====#
#====----OPENING PAGE----=====#
def destroyroot():
root.destroy()
LogInButton = Button(root, text="Log In", bg="#93ff00", command=newwindow).grid(row=1, column=0)
CloseRoot = Button(root, text="Close window", bg="#93ff00", command=destroyroot).grid(row=3, column=0)
root.mainloop()
#===--OPENING PAGE END---====#
#======================-------------END------------------======================#
答案 0 :(得分:2)
您没有正确设置file
变量。您可以在函数newwindow
中定义它,但是该变量未在其他函数中设置。此外,file
是python中的一种类型(例如int
,list
等...)。因此,您不应将其用作名称,而是错误的来源。您正在执行file.write
,因此它从类型而不是变量中获取write
,并且您会收到无关的错误。
答案 1 :(得分:0)
一些Python基础知识:
1)“来自Tkinter import *”是不好的形式。当你从几个模块“导入*”时,它会变得非常混乱。请尝试“导入Tkinter”。参考方法为“Tkinter.xxxx”。例如“personal = Tkinter.Toplevel(root)”。
2)“file”是Python中的一个类型,因此您永远不应该将它用作变量名。使用“str”和“len”作为变量名称是新Python用户的其他常见问题。
接下来,您的代码似乎与所有嵌套函数非常复杂。
如果您在不同的函数中共享变量组,那么您可以在类中做得更好。这是一个非常简单的示例类,它还显示了写入文件的正确方法:
class MyClass(object):
def __init__(self, file_name = r'c:\myfile.txt'):
self.file_name = file_name
def write_to_file(self, stuff_to_write):
with open(self.file_name, 'a') as f:
f.write(stuff_to_write)
def WriteLName(self, name):
text = "Last Name: %s\n"%name
self.write_to_file(text)
output = MyClass(r'c:\output_file.txt')
output.WriteLName('Jones')
制作一个名为c:\ output_file.txt的文件,其中包含“姓氏:琼斯”字样。
在函数中定义变量时,它不会自动“全局”,因此其他函数看不到它:
def open_file():
fout = open(r'c:\myfile.txt', 'w')
def write_file():
fout.write('some text\n')
open_file()
write_file()
当您点击“fout.write”行时出现此错误:
NameError: global name 'fout' is not defined
如果添加全局声明,它会按预期工作。许多人会说这是“糟糕的形式”,但作为现有代码的快速修复,可能需要尝试。
def open_file():
global fout
fout = open(r'c:\myfile.txt', 'w')
def write_file():
global fout
fout.write('some text\n')
open_file()
write_file()