Python + Tkinter,如何在后台独立于tk前端运行?

lsp*_*lsp 1 python multithreading tkinter

我是tkinter新手。

与tkinter popup(form?)分开,在后台运行长时间运行的进程的首选方式是什么?

我已经在使用tkinter时阅读了有关多线程的不同文章,但找不到简单的“这样做”。

需要明确的是,我需要的行为是,用户启动程序,tkinter位弹出,表示进程已启动。用户可以关闭此弹出窗口,而不会影响其余过程。也许当该过程完成后,我可以抛出另一个tkinter弹出窗口。

如果tkinter对此过于矫over,请随时提出更好的方法。

谢谢!

Nov*_*vel 5

Joran Beasley has the right answer, but way overcomplicated things. Here's the simple version:

class Worker(threading.Thread):
    def run(self):
        # long process goes here

w = Worker()
w.start()
tkMessageBox.showinfo("Work Started", "OK started working")
w.join()
tkMessageBox.showinfo("Work Complete", "OK Done")
Run Code Online (Sandbox Code Playgroud)

Edit: here's a working example of it:

import threading
import time
import tkMessageBox
import Tkinter as tk
root = tk.Tk()
root.withdraw()

class Worker(threading.Thread):
    def run(self):
        # long process goes here
        time.sleep(10)

w = Worker()
w.start()
tkMessageBox.showinfo("Work Started", "OK started working")
root.update()
w.join()
tkMessageBox.showinfo("Work Complete", "OK Done")
Run Code Online (Sandbox Code Playgroud)