首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用tkinter在我的窗口中创建实时更新的提要

如何使用tkinter在我的窗口中创建实时更新的提要
EN

Stack Overflow用户
提问于 2020-06-08 22:58:38
回答 1查看 79关注 0票数 0

我需要得到一个单独的窗口的一部分,以输出文本的基础上发生了什么,例如,4电子邮件发送将如下所示:发送...成功:已发送电子邮件!成功:已发送电子邮件!成功:已发送电子邮件!成功:已发送电子邮件!所有4封电子邮件都已成功发送!

代码语言:javascript
复制
import smtplib
from tkinter import *

root = Tk()

root.title("Inbox Flooder")
root.geometry("720x520+0+0")
root.configure(background="black")

def click():
    global YOUR_EMAIL_ADDRESS , YOUR_PASSWORD , AMNT_REPEAT , TARGET_EMAIL , subject , msg
    YOUR_EMAIL_ADDRESS=YOUR_EMAIL_ADDRESS.get()
    YOUR_PASSWORD=YOUR_PASSWORD.get()
    TARGET_EMAIL=TARGET_EMAIL.get()
    subject=subject.get()
    msg=msg.get()
    AMNT_REPEAT=AMNT_REPEAT.get()
    print("Starting...")    

while int(AMNT_REPEAT) > 0 :
    send_email(subject, msg)
    AMNT_REPEAT = int(AMNT_REPEAT) - 1

if failed > 0 and sent > 0 :
    print(sent,"email(s) have been sucessfully sent and",failed ,"emails have failed to send.")

if failed > 0 and sent == 0 :
    print("All",failed ,"emails have failed to send.")

if failed == 0 and sent > 0 :
    print("All",sent ,"emails have sucessfully been sent!")


def send_email(subject, msg):
    global sent , failed
    try:
        server = smtplib.SMTP('smtp.gmail.com:587')
        server.ehlo()
        server.starttls()
        server.login(YOUR_EMAIL_ADDRESS, YOUR_PASSWORD)
        message = 'Subject: {}\n\n{}'.format(subject, msg)
        server.sendmail(TARGET_EMAIL, TARGET_EMAIL, message)
        server.quit()
        print("Sucess: Email sent!")
        sent = sent + 1

    except:
        print("Error: Email failed to send.")
        failed = failed + 1

def close_window():
    try:
        root.destroy()
        exit()

    except:
        print("Tab Closed")
EN

回答 1

Stack Overflow用户

发布于 2020-06-09 03:34:08

下面是一个简单的程序,它在窗口打开时循环,添加“成功:发送电子邮件!”这只不过是演示如何向基本的tkinter GUI添加内容。

然而,你将需要使用线程,因为你的发送应该在窗口打开时在后台执行,这将需要一些适当的研究,我不会在这里解释这个过程(另一个线程应该很容易找到)。

代码语言:javascript
复制
import tkinter as tk

def send_success():
    # Allow us to insert into the widget
    logt.config(state="normal")
    # Insert into the widget (adding a newline at the end)
    logt.insert("end", "Success: Email sent!\n")
    # Disable user changes again
    logt.config(state="disabled")
    # Scroll to bottom of widget (to show new text)
    logt.see("end")
    # Schedule a call to ourselves after 1 second
    root.after(1000, send_success)

# Create the main tkinter window
root = tk.Tk()

# Create our text widget (normally a multi-line text
# entry widget, but this will be our log widget)
logt = tk.Text(root)
# Add the widget to the window
# (and set it to increase in size with the window)
logt.pack(side="left", fill="both", expand=True)
# Stop people changing the content
logt.config(state="disabled")

# Create a vertical scrollbar
sc = tk.Scrollbar(root)
# Add the widget to the window
sc.pack(side="right", fill="y")
# Move the text widget when scrollbar moved
sc.config(command=logt.yview)
# Change scrollbar length when new content added
logt.config(yscrollcommand=sc.set)

# Schedule our method to run once the mainloop is running
root.after(0, send_success)

# Run the mainloop
# This handles window actions (moving, resizing, etc.)
root.mainloop()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62264964

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档