Skip to content
Advertisement

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. The called function looks like the following:

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. 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. Sadly, I was not able to reverse engineer the spyder code with my current knowledge.

Edit 1: For clarification: I do not want any shell to be visible, only the 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.

Edit 2: Target OS is Windows.

The package is set up like the following way, so the command to start the app would be 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.

Thanks for your help.

Advertisement

Answer

The solution is to use gui_scripts as an entry point, not console_scripts. Should have read the documentation more carefully: Entry points specification

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement