亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將電子郵件添加到屏幕?

如何將電子郵件添加到屏幕?

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 關注
  • 187 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號