如何将电子邮件添加到屏幕?

我有这段代码,问题是当我完成程序并重新启动时,它不会将电子邮件保存在屏幕上,只保存在email.txt.


我怎样才能在屏幕上添加电子邮件和密码,因为即使我重新启动文件,电子邮件仍然出现在屏幕上,而不仅仅是在email.txt?


from tkinter import *

from tkinter import messagebox

import tkinter.messagebox


roots = Tk()

roots.title("Email's save")

roots.geometry("500x500")


e = Entry(roots)

e.grid(row=0, column=1)

e.focus_set()


p = Entry(roots, show="*")

p.grid(row=1, column=1)

p.focus_set()


textEmail = StringVar()

textPassword = StringVar()



def callback():

    textEmail.set(textEmail.get() + e.get() + "\n")

    textPassword.set(textPassword.get() + p.get() + "\n")



def cleargrid():

    textEmail.set("")

    textPassword.set("")



def delete():

    answer = tkinter.messagebox.askquestion('Delete', 'Are you sure you want to delete this entry?')

    if answer == 'yes':

        cleargrid()


def save():

    email_info = e.get()

    password_info = p.get()


    file = open("emails.txt", "a")

    file.write(email_info)

    file.write("\n")

    file.write(password_info)

    file.write("\n")

    file.write("=" * 20)

    file.close()



def EmailPassword():

    email = Label(roots, text="Email: ", font=('Courier', 14))

    email.grid(row=0, sticky=W)


    passoword = Label(roots, text="Password: ", font=('Courier', 14))

    passoword.grid(row=1, sticky=W)


    saved_email = Label(roots, text="Saved Email", font=('Courier', 14))

    saved_email.grid(row=15, column=0)


    saved_password = Label(roots, text="Password", font=('Courier', 14))

    saved_password.grid(row=15, column=15)


    write_email = Label(roots, textvariable=textEmail, font=('Courier', 14))

    write_email.grid(row=20, column=0)


    write_password = Label(roots, textvariable=textPassword, font=('Courier', 14))

    write_password.grid(row=20, column=15)


    btn_save = Button(roots, text="Save", command= lambda:[callback(), save()])

    btn_save.grid(row=10, column=2, sticky=W)


    btn_del = Button(roots, text="X", fg="red", command=delete)

    btn_del.grid(row=60, column=20)


    roots.mainloop()


EmailPassword()


UYOU
浏览 149回答 2
2回答

繁花不似锦

删除您当前的“emails.txt”。它的格式不正确,无法使以下内容正常工作。改成save这样。注意\n你的=*20def save():    with open("emails.txt", "a") as f:        f.write(f'{e.get()}\n{p.get()}\n{"="*20}\n')添加此功能def get_emails():    try:        with open("emails.txt", "r") as f:            for i, line in enumerate(filter(lambda t: t != f'{"="*20}\n', f.readlines())):                if not i%2:                    textEmail.set(f'{textEmail.get()}{line}')                else:                    textPassword.set(f'{textPassword.get()}{line}')    except FileNotFoundError:        pass在前面添加这一行roots.mainloop()get_emails()在旁边:您真的要将未加密的电子邮件和密码信息存储在文本文件中吗?

RISEBY

为了使电子邮件地址出现在开头,您必须从文件中获取该信息。只需添加另一个打开文件(如果存在)、读取地址并设置变量的函数textEmaildef set_email():    try:        file = open("emails.txt", "r")        emails = file.readlines()        last_address = emails[-2][:-1] # line before last line without the line break        file.close()        textEmail.set(last_address)    except:        pass ## There was no file "emails.txt"如果在定义变量后调用此函数textEmail,您将获得窗口加载时的地址。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python