Python + Tkinter,如何独立于tk前端在后台运行? [英] Python+Tkinter, how to run in background independant of tk frontend?

查看:906
本文介绍了Python + Tkinter,如何独立于tk前端在后台运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是tkinter新手.

I'm a tkinter noob.

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

What would be the preferred way to run a long-running process in the background, separate from my tkinter popup(form?)?

在使用tkinter时,我已经阅读了有关多线程的不同内容,并且找不到简单的这样做".

I've read different things about multithreading while using tkinter and can't find a straightforward "do it like this".

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

To be clear, the behavior I need is, user starts program, tkinter bit pops up saying the process has started. User can dismiss this popup without affecting the rest of the processes. Maybe when the process has finished I can throw up another tkinter popup.

如果tkinter在这方面过大,请随时提出一个更好的方法.

If tkinter is overkill for this, please feel free to suggest a better approach.

谢谢!

推荐答案

Joran Beasley的答案正确,但方法过于复杂.这是简单的版本:

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")

这是一个可行的示例:

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")

这篇关于Python + Tkinter,如何独立于tk前端在后台运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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