Python:无限循环和 GUI

发布于 2024-11-30 01:12:50 字数 209 浏览 2 评论 0原文

我正在尝试使用 wxPython GUI 编写一个 python 程序。程序必须在后台收集一些信息(无限循环),但此时 GUI 应该处于活动状态。就像,如果我单击某个按钮,某些变量或其他信息必须更改,并且在新周期中应该使用该变量而不是旧变量。

但我不知道,怎么做。我认为我必须使用线程,但我不明白如何使用它。

任何人都可以建议如何解决这个问题?

提前致谢!

I'm trying to write a python program with wxPython GUI. Program must to collect some information in background (infinite loop), but GUI should be active at this time. Like, if I click on the some button, some variable or another information must change, and at the new cycle this variable should be used instead of the old.

But I don't know, how to make it. I think that I must use threading, but I don't understand how to use it.

Anyone can suggest how to solve this problem?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

揽清风入怀 2024-12-07 01:12:50

您肯定需要使用线程来完成此任务。然后当你从非GUI线程获取一些数据时,你可以使用wxPython的线程安全方法之一来让它知道它需要更新。这是一个小教程: http://www.blog.pythonlibrary .org/2010/05/22/wxpython-and-threads/

或者长期最喜欢的:http://wiki.wxpython.org/LongRunningTasks

另一种方法是使用Python的套接字模块创建一个套接字服务器并以这种方式与wx通信。

You definitely need to use threads to accomplish this. Then when you get some data from the non-GUI thread, you can use one of wxPython's thread-safe methods to let it know that it needs to update. Here's a little tutorial: http://www.blog.pythonlibrary.org/2010/05/22/wxpython-and-threads/

Or the perennial favorite: http://wiki.wxpython.org/LongRunningTasks

Another way to do it would be to create a socket server using Python's socket module and communicate with wx that way.

拥抱没勇气 2024-12-07 01:12:50

这称为“线程化”。使用 python 线程模块

两个示例:

示例 1

from threading import Thread

class MyCollector(Thread):

    def __init__(self, collect_from):
        Thread.__init__(self) # must be called !
        self.collect_from = collect_from

    def run(self):
        while True:
            # .. collect ur things


collector_thread = MyCollector(my_source_to_collect_from)
collector_thread.start()

# go on with gui

示例2

from threading import Thread

def collector(collect_from):
    while True:
        # .. collect ur things

collector_thread = Thread(target = collector, args = (my_source_to_collect_from,))
collector_thread.start()

# go on with gui

This is called "threading". Use pythons threading module.

Two examples:

example 1:

from threading import Thread

class MyCollector(Thread):

    def __init__(self, collect_from):
        Thread.__init__(self) # must be called !
        self.collect_from = collect_from

    def run(self):
        while True:
            # .. collect ur things


collector_thread = MyCollector(my_source_to_collect_from)
collector_thread.start()

# go on with gui

example 2:

from threading import Thread

def collector(collect_from):
    while True:
        # .. collect ur things

collector_thread = Thread(target = collector, args = (my_source_to_collect_from,))
collector_thread.start()

# go on with gui
遗心遗梦遗幸福 2024-12-07 01:12:50

您是否考虑过让 wxPython 定期调用您的事件处理程序,并在其中执行后台处理?当然,这取决于您能否将您的工作划分为离散的部分。请注意,您的后台处理必须是非阻塞的,以便控制权能够及时返回到 wxPython,以允许响应式 GUI 处理。不确定在 wxPython 中实现此类后台处理的惯用方法是什么,但如果我没记错的话,(Py)Qt 中的技术是使用计时器。

Have you considered having wxPython invoke an event handler of yours periodically, and perform the background processing in this? This depends on you being able to divide your work into discrete pieces, of course. Be aware that your background processing would have to be non-blocking so control would return to wxPython in a timely manner, to allow responsive GUI processing. Not sure what's the idiomatic way to implement such background processing in wxPython, but if I recall correctly the technique in (Py)Qt was to use a timer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文