用进度条线程化Tkinter

时间:2015-08-31 12:45:32

标签: python multithreading python-2.7 tkinter queue

我试图制作一个将视频上传到服务器的线程Tkinter应用程序,问题是当我制作线程应用程序时,它以无限循环结束。这是代码:

class ThreadedTask(threading.Thread):
print "threaded task start"
    def __init__(self, queue):
        threading.Thread.__init__(self)
        self.queue = queue
    def start_upload(self):
        PATH = os.path.abspath(os.path.join(os.path.dirname(__file__),     "upload_video_to_server.py"))
        if os.path.isfile(PATH) and os.access(PATH, os.R_OK):
           print filename
           upload_video_to_server.make_Info(filename=filename,     title=video_title, description=video_desc, tags=video_keywords)
           upload_video_to_server.upload_Start(filename=filename)
           print "ok"
        else:
            tkMessageBox.showerror(
                "Error!",
                "Unable to find upload script!"
            )
            return

def tb_click():
    print "tbclick start"
    progress()
    prog_bar.start()
    global queue
    queue = Queue.Queue()
    ThreadedTask(queue).start()
    app.after(100, process_queue)

def process_queue():
        try:
            print "try pass"
            msg = queue.get(0)
            print "MSG: ", msg
            prog_bar.stop()
        except Queue.Empty:
            print "Queue is empty!"
            app.after(100, process_queue)

def progress():
    global prog_bar
    print "prog bar is being made"
    prog_bar = ttk.Progressbar(
            app, orient="horizontal",
            length=200, mode="indeterminate"
            )
    prog_bar.pack(side=TOP)

button1 = Button(app, text="START", width=20, command=tb_click)
button1.place(x=355, y=420)
app.mainloop()

运行此代码后,我立即得到"线程任务启动"打印出来,点击" START"按钮我进入无限循环打印"尝试通过"和#34;队列是空的" ...当我点击开始按钮(视频上传脚本不是从这段代码运行)时,什么都没有(最终用户可见)

这段代码有什么问题?有没有我错过的东西?

1 个答案:

答案 0 :(得分:0)

看起来永远不会执行start_upload。尝试将其重命名为run。然后在调用start()时执行。

你可能仍会得到“Queue is empty”消息,因为你似乎没有向队列中添加任何东西。