How can I create a GUI application that also provides a CLI without having a popup shell using PyInstaller?
For example, if I create the following application with pyinstaller argparse_gui.py --noconsole
, stdout isn't displayed in the shell:
C:\projects\argparse_gui\dist\argparse_gui>argparse_gui.exe -V
C:\projects\argparse_gui\dist\argparse_gui>
I can redirect stdout/stderr to a file with argparse_gui.exe -V > log.txt 2>&1
, but that's not exactly user-friendly. I can see stdout if built without --noconsole
, but then there's a nagging separate shell window.
# argparse_gui.py
import sys
import argparse
from PyQt5 import QtCore, QtWidgets, QtGui
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.init_widgets()
self.init_layout()
def init_widgets(self):
self.label = QtWidgets.QLabel('Hello, world!')
def init_layout(self):
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.label)
centralWidget = QtWidgets.QWidget()
centralWidget.setLayout(layout)
self.setCentralWidget(centralWidget)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-V", "--version", help="display application information", action='store_true')
args = parser.parse_args()
if args.version:
print('Version 123', flush=True)
else:
app = QtWidgets.QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())