如何在 Python 中从命令行控制/驱动/交互 PyQt GUI [英] How to control/drive/interact a PyQt GUI from command line in Python

查看:24
本文介绍了如何在 Python 中从命令行控制/驱动/交互 PyQt GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 PyQt 创建的 GUI 应用程序,我希望也能够通过一种内部 API 从 python 终端控制它.

I have a GUI application created with PyQt and I would like to be able to control it also from the python terminal through a kind of internal API.

想法:

  • 使用主终端:不可能,因为它被 QApplication 阻止(通过 app.exec_())
  • 在另一个线程中启动 GUI 以释放主线程:不可能,必须在主线程中执行 QApplications.
  • ???

我不想要应用内"终端.

I don't want an 'in-app' terminal.

您还有其他想法吗?

推荐答案

使用主终端:不可能,因为它被QApplication (by app.exec_())

Using the main terminal : impossible since that it is blocked by the QApplication (by app.exec_())

如果您在 Python 终端中并且不会阻塞,则可以省略 app.exec().正如此处所解释的那样,这是有效的,因为...

You can omit app.exec() if you are within a Python terminal and it will not be blocking. As explained here this works because...

PyQt5 安装一个输入钩子(使用 PyOS_InputHook)来处理当交互式解释器等待用户输入时发生的事件.这例如,意味着您可以从 Python shell 创建小部件提示,与他们交互,仍然可以输入其他Python命令.

PyQt5 installs an input hook (using PyOS_InputHook) that processes events when an interactive interpreter is waiting for user input. This means that you can, for example, create widgets from the Python shell prompt, interact with them, and still being able to enter other Python commands.

例如,在 Python shell 中输入以下内容以同时拥有一个有效的 Qt 小部件和一个非阻塞的 REPL.

For example, enter the following in the Python shell to have a working Qt widget and a non-blocking REPL simultaniously.

$> python
Python 3.7.6 | packaged by conda-forge | (default, Jan  7 2020, 22:05:27)
[Clang 9.0.1 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from PyQt5.QtWidgets import QApplication, QWidget
>>> a = QApplication([])
>>> w = QWidget()
>>> w.show()
>>> w.raise_()

IPython 具有类似的功能.如果你用 ipython --gui=qt 启动它,或者在终端输入 %gui qt 你会得到同样的效果...

IPython has similar functionality. If you start it with ipython --gui=qt, or type %gui qt in the terminal you get the same effect...

$> ipython
Python 3.7.6 | packaged by conda-forge | (default, Jan  7 2020, 22:05:27)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.11.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: %gui qt

In [2]: from PyQt5 import QtWidgets

In [3]: win = QtWidgets.QPushButton("click me")

In [4]: win.show()

In [5]: win.raise_()

我建议使用 IPython,因为它更适合交互式工作,并且可以与 PySide 一起使用(也许常规 Python 和 PySide 也可以;我没有检查).

I recommend to use IPython because it's better suited for interactive work and it will work with PySide (perhaps regular Python and PySide will also work; I didn't check).

另见我之前的回答这里

最后,即使这样有效,我也不知道性能有多好.对于业余爱好项目来说,这是一个很好的解决方案,但如果您有很多用户,我会考虑实施应用内"终端或某种形式的进程间通信.

Finally, even though this works I don't know how good the performance is. It's a good solution for a hobby project but if you have many users I would consider to implement an 'in-app' terminal or some form of inter-process communication.

这篇关于如何在 Python 中从命令行控制/驱动/交互 PyQt GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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