简体   繁体   English

Python:有什么方法可以在后台运行mainloop()吗?

[英]Python: Is there a way I can run mainloop() in the background?

Is there a way I can run mainloop() in the background? 有没有办法可以在后台运行mainloop()? I don't want to add the complexity of threads. 我不想增加线程的复杂性。 Thanks in advance. 提前致谢。

否。GUI框架的主循环必须始终在前台运行。

No, you can't do this. 不,你不能这样做。 The message loop runs in its thread and processes your message queue. 消息循环在其线程中运行并处理您的消息队列。

There isn't really any official distinction between background and foreground threads. 后台线程和前台线程之间实际上并没有任何正式的区别。 The thread that processes your message is typically called the foreground but it's not really any different from any other thread. 处理您的消息的线程通常称为前台线程,但它与其他任何线程实际上并没有什么不同。 It's only convention that leads us to refer to threads foreground or background. 唯一的约定使我们引用线程的前台或后台。

Ultimately, that main thread with the message loop in has the message loop at the top of its call stack and that's just the way it has to be. 最终,带有消息循环的主线程在其调用堆栈的顶部具有消息循环,这就是它必须这样做的方式。

It is possible to start a long running task and get it to frequently process messages, but this way requires a keen understanding of re-entrancy issues and often leads to insanity. 可以启动一个长期运行的任务并使其频繁处理消息,但是这种方式需要对重新进入问题有敏锐的理解,并经常导致精神错乱。

If you want long running background tasks then you probably need threads. 如果要长时间运行后台任务,则可能需要线程。

What about keeping your message loop in the foreground, and your other processing in the background (say, with an after method)? 如何将消息循环保持在前台,而将其他处理保持在后台(例如,使用after方法)呢? Especially if you don't want to use threads. 特别是如果您不想使用线程。 However, I think threads or processes will probably work better in the long run. 但是,我认为线程或进程从长远来看可能会更好地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在 python 的后台主循环有问题 - I have a problem with my mainloop in the background in python 我可以使用多处理从流程运行App.MainLoop吗? - Can I run App.MainLoop from a Process using multiprocessing? 有没有办法在 tkinter、python 中更改“.mainloop()”? - Is there a way of changing '.mainloop()' in tkinter, python? 如何在不中断tkinter主循环的情况下运行函数,又如何从该函数向主循环中的小部件发送信息? - How can I run a function without interrupting my tkinter mainloop, but also send information from that function to widgets in my mainloop? 使用 Python 和 Tkinter,我将如何运行 .mainloop() 的每个循环的代码? - Using Python and Tkinter, How would I run code every loop of .mainloop()? 我可以允许 tkinter 的 mainloop() 和 GLUT 的 glutMainLoop() 同时运行吗? - Can I allow tkinter's mainloop() and GLUT's glutMainLoop() run concurrently? 如何测试带有 Tkinter 主循环的 Python 代码? - How can Python code with Tkinter mainloop be tested? Python,Tkinter:如何使用线程防止tkinter gui mainloop崩溃 - Python, Tkinter: how can I prevent tkinter gui mainloop crash using threading Python:Celery,我怎样才能让它在后台运行? - Python : Celery, How can I make it run in background? 我可以在另一个循环的后台运行一个循环吗? 蟒蛇 - Can I run a loop in the background of another loop? Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM