Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从tkinter输入框保存到文本文件,而不将PY_VAR0作为值?_Python_File_Tkinter - Fatal编程技术网

Python 如何从tkinter输入框保存到文本文件,而不将PY_VAR0作为值?

Python 如何从tkinter输入框保存到文本文件,而不将PY_VAR0作为值?,python,file,tkinter,Python,File,Tkinter,我对Python很有信心,但从未使用过tkinter,因此我非常感谢您的帮助/ 我正在尝试使用tkinter和文本文件创建一个基本的登录系统。当我保存到文本文件时,它只是将PY_VAR0和PY_VAR1保存到文本文件,我尝试使用.get,但错误表明str没有属性。get 我尝试过很多东西,但都没有运气——我有一种感觉,我做错了什么,这是显而易见的,我只是没有意识到而已 有些代码在这里可能完全没有用处,但我想我会将其全部包含在内,因为它可能会帮助您找出导致问题的原因 有人能帮忙吗 # import

我对Python很有信心,但从未使用过tkinter,因此我非常感谢您的帮助/

我正在尝试使用tkinter和文本文件创建一个基本的登录系统。当我保存到文本文件时,它只是将PY_VAR0和PY_VAR1保存到文本文件,我尝试使用.get,但错误表明str没有属性。get

我尝试过很多东西,但都没有运气——我有一种感觉,我做错了什么,这是显而易见的,我只是没有意识到而已

有些代码在这里可能完全没有用处,但我想我会将其全部包含在内,因为它可能会帮助您找出导致问题的原因

有人能帮忙吗

# importing librarys
import tkinter
from tkinter import *
from functools import partial

#setting up window
window = tkinter.Tk()
window.title('Widgets Example')
window.title('All Planner_login')
window.configure(width=355, height=355)

def validateLogin(username, password):
    print("username entered :", username.get())
    print("password entered :", password.get())



winWidth = window.winfo_reqwidth()
winwHeight = window.winfo_reqheight()
posRight = int(window.winfo_screenwidth() / 2 - winWidth / 2)
posDown = int(window.winfo_screenheight() / 2 - winwHeight / 2)
window.geometry("+{}+{}".format(posRight, posDown))

fr = tkinter.Frame(window, height=175, width=295, bg='#1f85de', borderwidth=75)
fr.place(relx=.5, rely=.5, anchor="c")
lblInst = tkinter.Label(fr, text='log in:', fg='#000000', bg='#1f85de', font=('Helvetica', 16))
lbl = tkinter.Label(fr, text="Username:", bg='#1f85de')
username = StringVar()
ent = tkinter.Entry(fr, textvariable=username)
lbl2 = tkinter.Label(fr, text='Password:', bg='#1f85de')
password = StringVar() 
ent2 = tkinter.Entry(fr,show='*', textvariable=password)
validateLogin = partial(validateLogin, username, password)
btn = tkinter.Button(fr, text='Login', command=validateLogin, fg='#a1dbcd', bg='#1aa831')

usernameSave=str(username)
passwordSave=str(password)

text_file = open("passdoc.txt", "w")
n = text_file.write(usernameSave)
text_file.close()

text_file = open("passdoc.txt", "w")
n = text_file.write(passwordSave)
text_file.close()




lblInst.pack()
lbl.pack()
ent.pack()
lbl2.pack()
ent2.pack()
btn.pack()
window.configure(background='#6b18ba')
window.mainloop()



#menu window
from tkinter import *
window=Tk()
window.title('ALL Planner')
window.configure(width=1000, height=600)
window.configure(background='#6b18ba')

winWidth = window.winfo_reqwidth()
winwHeight = window.winfo_reqheight()
posRight = int(window.winfo_screenwidth() / 2 - winWidth / 2)
posDown = int(window.winfo_screenheight() / 2 - winwHeight / 2)
window.geometry("+{}+{}".format(posRight, posDown))

fr = tkinter.Frame(window, height=500, width=900, bg='#1f85de', borderwidth=75)
fr.place(relx=.5, rely=.5, anchor="c")
lbl = tkinter.Label(fr, text="Notes:", bg="#6b18ba")


window.mainloop()
 

请尝试
usernameSave=str(username.get())
passwordSave=str(password.get())
请尝试将此代码减少到一个最小值。根据对问题的描述,听起来我们只需要一个条目小部件,一个获取数据并保存数据的函数,以及足够的额外代码就可以工作。如果您发布了所得到的完整错误,也会有所帮助。请像在
validateLogin
中那样使用变量。不管怎样,这都是保存值的好地方。您使用
n=text\u file.write(usernameSave)
StringVar()
的实例写入文本文件,您应该做的是@Mike67所说的,因为您希望获取值并将其写入文件。此外,不建议使用多个
Tk()
实例,将所有子窗口替换为
Toplevel()