TTK进度条在发送电子邮件时被阻止 [英] TTK progress bar blocked when sending email

查看:138
本文介绍了TTK进度条在发送电子邮件时被阻止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用tkinter在python中编写一个应用程序。在这个应用程序中,我想发送一批电子邮件,我想在发送进度条时显示一个进度条。我可以创建进度条并启动它,但是当发送电子邮件时,该栏就停止移动(如果在发送电子邮件之前已经开始,我想在发送电子邮件之前启动该栏,但是它只是挂起来,当我这样做时,酒吧上没有任何动作。

  startProgressBar()
sendEmails )
stopProgressBar()

我已经尝试将电子邮件发送到一个单独的线程,但我似乎没有任何运气,我正在使用高级线程模块,有什么建议可以做什么?也许我没有得到线程部分正确,我正在使用smtplib发送电子邮件。

解决方案

这是一个旧问题,但我所指的代码食谱帮助了我一个类似的概念,所以我想它应该被共享。



这种类型的问题需要使用线程,以便我们将更新GUI和d了解实际任务(如发送电子邮件)。看看Active State的这个代码配方,我相信这正是你正在寻找的作为在线程之间通过线程(通过队列)传递信息的示例。



我尝试从代码配方中突出显示重要的部分。我不包括设置进度条本身,而是整体代码结构和获取/设置队列。

  import Tkinter 
导入线程
import队列

class GuiPart:
def __init __(self,master,queue,endCommand):
self.queue = queue
#在这里设置GUI(即绘制进度条)

#家伙处理队列内容
def processIncoming(self):
while self.queue.qsize():
try:
#从队列中获取一个值(电子邮件进度)
progress = self.queue.get(0)
#在这里更新进度条。

除了Queue.Empty:
pass

class ThreadedClient:
#启动Gu并发送电子邮件任务
def __init __( self,master):
self.master = master
self.queue = Queue.Queue()

#设置Gui,参考代码食谱
self .gui = GuiPart(master,self.queue,...)

#设置异步线程(设置标志告诉我们我们正在运行)
self.running = 1
self.email_thread = threading.Thread(target = self.send_emails)
self.email_thread.start()

#开始检查队列
self.periodicCall()

def periodicCall(self):
#检查队列的内容
self.gui.processIncoming()
#等待X毫秒,再次调用...(见代码食谱

def send_emails(self):#AKAworker thread
while(self.running) :
#发送电子邮件
#计算电子邮件进度的%age

#将此值放入队列!
self.queue.put(value)

#最终用完电子邮件发送。
def endApplication(self):
self.running = 0


root = Tkinter.Tk()
client = ThreadedClient(root)
root.mainloop()


I am writing an app in python using tkinter. In this app I am trying to send out a batch of emails and I want to show a progress bar while they are being sent. I am able to create the progress bar and start it, but when the emails are being sent, the bar just stops moving (If it is started way before the emails are sent, I want to start the bar just before the emails are sent, but it just hangs and nothing moves on the bar when I do it like this.

startProgressBar()
sendEmails()
stopProgressBar()

I have tried putting the sending of emails into a separate thread, but I don't seem to be having any luck. I am using the high-level Threading module. Are there any suggestions on what to do? Perhaps I am not getting the threading part correct. I am using the smtplib to send emails.

解决方案

This is an old question but the code recipe I'm referring to has helped me with a similar concept, so I thought it should be shared.

This type of problem needs to use threading so that we spilt up the job of updating the GUI and doing the actual task (such as sending emails). Have a look at this code recipe from Active State, I believe it's exactly what you're looking for as an example of threading and passing information between threads (via a queue).

I try to highlight the important parts from the code recipe. I don't include setting up the progress bar itself but rather the overall code structure and getting/setting a queue.

import Tkinter
import threading
import Queue

class GuiPart:
    def __init__(self, master, queue, endCommand):
        self.queue = queue
        # Do GUI set up here (i.e. draw progress bar)

        # This guy handles the queue contents
        def  processIncoming(self):
            while self.queue.qsize():
                try:
                    # Get a value (email progress) from the queue 
                    progress = self.queue.get(0)
                    # Update the progress bar here.

                except Queue.Empty:
                    pass

class ThreadedClient:
    # Launches the Gui and does the sending email task
    def __init__(self, master):
        self.master = master
        self.queue = Queue.Queue()

        # Set up the Gui, refer to code recipe
        self.gui = GuiPart(master, self.queue, ...)

        # Set up asynch thread (set flag to tell us we're running)
        self.running = 1        
        self.email_thread = threading.Thread(target = self.send_emails)
        self.email_thread.start()

        # Start checking the queue
        self.periodicCall()

     def periodicCall(self):
         # Checks contents of queue
         self.gui.processIncoming()
         # Wait X milliseconds, call this again... (see code recipe)

     def send_emails(self): # AKA "worker thread"
         while (self.running):
             # Send an email
             # Calculate the %age of email progress

             # Put this value in the queue!
             self.queue.put(value)

     # Eventually run out of emails to send.
     def endApplication(self):
         self.running = 0


root = Tkinter.Tk()
client = ThreadedClient(root)
root.mainloop()

这篇关于TTK进度条在发送电子邮件时被阻止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆