为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

UYOU 2023-06-06 16:41:26
我有这段代码,问题是当我完成程序并重新启动时,它不会将电子邮件保存在屏幕上,只保存在email.txt.我怎样才能在屏幕上添加电子邮件和密码,因为即使我重新启动文件,电子邮件仍然出现在屏幕上,而不仅仅是在email.txt?from tkinter import *from tkinter import messageboximport tkinter.messageboxroots = 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()
查看完整描述

2 回答

?
繁花不似锦

TA贡献1851条经验 获得超4个赞

删除您当前的“emails.txt”。它的格式不正确,无法使以下内容正常工作。

改成save这样。注意\n你的=*20

def 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()

在旁边:


您真的要将未加密的电子邮件和密码信息存储在文本文件中吗?


查看完整回答
反对 回复 2023-06-06
?
RISEBY

TA贡献1856条经验 获得超5个赞

为了使电子邮件地址出现在开头,您必须从文件中获取该信息。只需添加另一个打开文件(如果存在)、读取地址并设置变量的函数textEmail


def 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,您将获得窗口加载时的地址。


查看完整回答
反对 回复 2023-06-06
  • 2 回答
  • 0 关注
  • 116 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信