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

该博客介绍了如何在Tkinter GUI应用中启动一个后台长运行过程,使得用户可以关闭窗口而不会影响进程。通过创建一个继承自`threading.Thread`的Worker类并在其中实现长运行过程,然后启动线程并显示消息框通知用户,实现了这一目标。当过程结束时,再次使用消息框提示用户。
摘要由CSDN通过智能技术生成

I'm a tkinter noob.

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

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

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.

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

Thanks!

解决方案

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值