PyQT:我如何处理 main 中的循环? [英] PyQT: How do I handle loops in main?

查看:60
本文介绍了PyQT:我如何处理 main 中的循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 PyQT GUI 的应用程序.我以通常的 PyQT 方式设置了 GUI:

I have an application that has a PyQT GUI. I set up the GUI in the usual PyQT fashion:

app = QtGui.QApplication(sys.argv)
win = MainWindow()
win.initialize()
win.show()
sys.exit(app.exec_())

问题是在这之后我有一个处理信号的 while 循环(signal.signal(signal.SIGINT, ...) 并且还做一些其他的事情.如果我调用 sys.exit(app.exec_()) 在 while 循环之前,循环不执行.如果我在循环之后调用它,GUI 就会挂断.非常感谢任何帮助!!

The problem is that right after this I have a while loop that handles signals (signal.signal(signal.SIGINT, ...) and also does some other things. If I call sys.exit(app.exec_()) before the while loop, the loop does not execute. If I call it after the loop, the GUI hangs up. Any help is much appreciated!!

推荐答案

app.exec_() 调用基本上启动了处理 [QT] 信号的 while 循环"等.它是事件循环程序.

The app.exec_() call basically starts 'a while loop that handles [QT] signals' etc. It's the event loop of the program.

你不应该需要自己的循环来做这些事情.如果你这样做,你在谈论多线程,你需要查看 QThread<的文档/a> 和/或 QEventLoop.

You shouldn't need your own loop to do that stuff. If you do, you're talking about multiple threads and you need to look at the docs for QThread and/or QEventLoop.

真的不应该需要一个while循环来处理SIGINT等系统信号——这些是专门设计的,这样你就可以像事件一样挂钩"它们,它们的发生会触发你的功能指定.

You really shouldn't need a while loop to handle system signals like SIGINT etc - these are specifically designed so that you 'hook into' them like events, and their occurrence will trigger a function you specify.

这篇关于PyQT:我如何处理 main 中的循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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