简体   繁体   English

如何在不阻塞的情况下从 shell 启动 QApplication?

[英]How to start a QApplication from shell without blocking it?

I have a simple Qt5 Application which can be installed as a pip package. The package has an entry point so i can start the application via a command prompt.我有一个简单的 Qt5 应用程序,它可以安装为 pip package。package 有一个入口点,因此我可以通过命令提示符启动应用程序。 The called function looks like the following:被调用的 function 如下所示:

def start_app():
    app = QtWidgets.QApplication(sys.argv)

    win = MainWindow()
    win.show()

    app.exec()
    sys.exit()

And the MainWindow (minimal working example):和主窗口(最小工作示例):

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

The problem is that now the command prompt IS the main application, so if i close the prompt, the python app closes too.问题是现在命令提示符是主应用程序,所以如果我关闭提示符,python 应用程序也会关闭。 Furthermore the command prompt is "blocked", so no additional commands can be entered.此外,命令提示符被“阻止”,因此无法输入其他命令。

My goal is to just use the command prompt to start the app and close it after or use it for other commands.我的目标是仅使用命令提示符启动应用程序并在之后关闭它或将其用于其他命令。 As an example we can take the Spyder app, which can be started via the command spyder , which does not block the command prompt.以 Spyder 应用程序为例,它可以通过命令spyder启动,它不会阻止命令提示符。 Sadly, I was not able to reverse engineer the spyder code with my current knowledge.遗憾的是,我无法以我目前的知识对 spyder 代码进行逆向工程。

Edit 1 : For clarification: I do not want any shell to be visible, only the Qt GUI.编辑 1 :为了澄清:我不想看到任何 shell,只有 Qt GUI。 Also i want to achieve to open the app with a single command (like spyder ) without any command-line parameters, like you see in the comments.我还想实现使用单个命令(如spyder )打开应用程序而无需任何命令行参数,就像您在评论中看到的那样。

Edit 2: Target OS is Windows.编辑 2:目标操作系统是 Windows。

The package is set up like the following way, so the command to start the app would be start_app . package 的设置方式如下,因此启动应用程序的命令为start_app

setup(
    ...
    entry_points = {
        'console_scripts': ['start_app = package.module:start_app']
    },
    ...
)

I've tried using QThreads and looked up similar questions but could not get it running, not even close.我试过使用 QThreads 并查找了类似的问题,但无法让它运行,甚至没有关闭。

Thanks for your help.谢谢你的帮助。

If you are on a bash shell you can use CTRL+Z to send a running process to background.如果您使用的是 bash shell,则可以使用CTRL+Z将正在运行的进程发送到后台。 Or run your app with a trailing & at the end of the command to start the process in background from beginning.或者在命令末尾使用&运行您的应用程序,以从头开始在后台启动进程。

The solution is to use gui_scripts as an entry point, not console_scripts .解决方案是使用gui_scripts作为入口点,而不是console_scripts Should have read the documentation more carefully: Entry points specification应该更仔细地阅读文档: 入口点规范

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 python 或 Shell 代码退出 Qapplication (PyQt) - How to Quit Qapplication (PyQt) with python or Shell code 如何从tkinter菜单启动带有参数的程序而不会阻塞? - How can I start a program from a tkinter menu with an argument without blocking? 如何在不阻塞 Django 服务器的情况下启动线程并等待其结束? - how to start threads and wait for their end without blocking a Django server? 如何在不阻塞的情况下从控制台获取输入? - How to get input from the console without blocking? 如何在不退出python脚本的情况下销毁一个QApplication然后运行一个新的? - How to destroy a QApplication and then run a new one without exiting the python script? 如何从 python 脚本启动 shell - How to start a shell from a python script 从Qapplication事件循环创建新的Qapplication - Creating a new Qapplication from Qapplication event loop 开始抓取多个蜘蛛,而不会阻止该过程 - Start scrapy multiple spider without blocking the process 如何在 Python Paramiko 中启动没有终端仿真的 shell? - How do I start a shell without terminal emulation in Python Paramiko? 如何使用importlib动态地从PySide2导入QApplication - How to use importlib to import QApplication from PySide2 dynamically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM