PyQt:app.exec_() 停止所有以下代码运行 [英] PyQt: app.exec_() stops all following code from running

查看:179
本文介绍了PyQt:app.exec_() 停止所有以下代码运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的代码:

I have a code which looks like:

app = QApplication(sys.argv)
self.interface = Interface()

# The figure
self.fig = self.interface.fig
self.ax = self.fig.add_subplot(111)

self.interface.show()
app.exec_()

print 'this is not printed'

问题是一旦 app.exec_() 被执行,在我关闭弹出的窗口之前什么都没有.

The problem is that once app.exec_() is executed, nothing is until I close the window that pops up.

如何继续运行代码?

推荐答案

这是预期的.您需要做的是在调用 app.exec() 之前使用信号/插槽、Qt 类中的代码或生成线程.

That is intended. What you have to do is use signals/slots, code inside your Qt classes, or spawn off threads before you call app.exec().

信号和槽是您与 Qt 交互的实际方式.基本上,信号是任何事件"或自定义事件",槽可以被认为是事件处理程序".例如,当有人点击 GUI 上的按钮时,它会创建一个信号,寻找连接到它的任何处理程序.您可以将一个、一个或多个插槽连接到每个信号(您甚至可以多次连接相同的一个)!这是一个很好的参考 Python 中的这个.

Signals and slots are your defacto way of interacting with Qt. Basically a signal is any "event" or custom "event" and slots can be thought of as "event handlers". For instance when someone hits a button on a GUI it creates a signal that seeks out any handler that is connected to it. You can connect none, one, or many slots to each signal (you can even connect the same one multiple times)! Here is a good reference for this in python.

在 Qt 类中编码通常意味着创建对您有用的插槽.请记住,您不想让事件循环拖得太久,因此如果您这样做,请创建一个新线程.

Coding inside your Qt classes usually means creating slots that do useful work for you. Remember you don't want to hold up the event loop too long so spawn a new thread if you do this.

您可以使用的第三个选项是剥离其他线程.从线程与 Qt 交互时要小心,如果你这样做,你必须使用信号和插槽.按照本 SO 中的建议实施线程.

The third option available to you is to spin off other threads. Be careful interacting with Qt from threads, if you do you MUST us signals and slots. Implement threading as suggested in this SO.

这篇关于PyQt:app.exec_() 停止所有以下代码运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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