首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何才能启动图形小部件,而不让控制流卡在python的主循环中?

如何才能启动图形小部件,而不让控制流卡在python的主循环中?
EN

Stack Overflow用户
提问于 2014-03-15 10:44:49
回答 1查看 114关注 0票数 0

我现在正在为即将到来的项目设计程序结构,我被困在以下问题上:

归结到它最重要的元素,我的程序被分成两个文件logic.pyoscilloscope.py

在logic.py中,我在/dev/input/..上初始化一个设备,并设置用于信号处理的滤波器等。在oscilloscope.py中,我有一个示波器实现,它使用Pygame来解释/显示来自注册输入设备的输入。

现在,我在logic.py中有了一个主循环,它不断地监听来自设备的输入,然后将处理过的数据转发到示波器,但我也有来自Pygame示波器实现的主循环。

当从logic.py初始化示波器时,如何防止程序控制流被卡在Pygame主循环中:

代码语言:javascript
复制
from oscilloscope import *
...
... #initializing filters to use, device to listen to etc.
...
self.osc = Oscilloscope(foo, bar) #creating an instance of an oscilloscope implemented with Pygame

self.osc.main() #calling the main loop of the oscilloscope. This handles all the drawing and updating screen.

#usually the flow would stop here as it is stuck in the Pygame main loop
#I need it to not get stuck so I can call the second main loop.

self.main() #captures and processes data from /dev/input/... sends processed data to the Pygame oscilloscope to draw it.

由于没有任何实际的代码,我希望这些评论澄清了我想要做的事情。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-16 13:40:52

(我知道我的建议可能只是理论上的):

self.osc.main()循环调用self.main()循环。要做到这一点,您必须编辑您的self.osc.main()函数,这样它就不会永远运行,而只能运行一次。保持self.main()循环不变,每个循环调用self.osc.main()一次。

而不是你现在正在做的事情,就像这样:

代码语言:javascript
复制
def main(): #This is the self.osc.main() function
    while True: #run forever
        doSomething()

def main(): #This is the self.main() function
    while True: #also run forever
        doSomethingElse()

你可以这样做:

代码语言:javascript
复制
def main(): #This is the self.osc.main() function
    doSomething() #notice I removed the loop. It only runs once. This is because the looping is handled by the other main() function (the one you call self.main())

def main(): #This is the self.main() function
    while True: #also run forever
        self.osc.main() #since we have now changed the self.osc.main() loop to run only once
        doSomethingElse()

我希望这能帮上忙。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22422711

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档