Search code examples
pythonexitpywinautosuppress-warnings

Python has stopped working when importing pywinauto package even I did not use it


I tried to use 'pywinauto' package and it works. But when I close app, python prompts an message "python has stopped working". I added sys.coinit_flags = 2 as suggested by Vasily Ryabov, the warning disappeared.

from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
import sys
import warnings
warnings.simplefilter("ignore", UserWarning)
sys.coinit_flags = 2
from pywinauto.application import Application
from pywinauto.keyboard import send_keys


class App(QWidget):

    def __init__(self):
        super(App,self).__init__()
        self.title = 'PyQt5 button - pythonspot.com'
        self.left = 10
        self.top = 100
        self.width = 320
        self.height = 200

        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        button = QPushButton('PyQt5 button', self)
        button.setToolTip('This is an example button')
        button.move(100, 70)
        button.clicked.connect(self.on_click)

        self.show()

    def on_click(self):
        print('PyQt5 button click')

def main():
    app = QApplication(sys.argv)
    app.setStyle('Fusion')

    w = App()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

But when I tried to make the code complex, the warning message was prompted again if I clicked on first combo box to select value 'ar-prod'.

    from PyQt5.QtCore import Qt
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

import sys
import warnings

warnings.simplefilter("ignore", UserWarning)
sys.coinit_flags = 2
from pywinauto.application import Application
from pywinauto.keyboard import send_keys


def main():
    app = QApplication(sys.argv)

    w = myView()
    w.show()
    sys.exit(app.exec_())



class myView(QWidget):
    def __init__(self):
        super(myView, self).__init__()

        self.setGeometry(400, 200, 1000, 700)

        self.mainLayout = QVBoxLayout()

        self.splitter1 = QSplitter(Qt.Horizontal)

        # Add ComboBox
        self.cb1=QComboBox()  #ar-dev or ar-prod
        self.cb1.addItems(['ar-dev','ar-prod'])
        self.cb2=QComboBox()  #ar-dev/.
        self.cb3=QComboBox()  #ar-dev/..
        self.cb4=QComboBox()  #ar-dev/...

        self.btn=QPushButton('Launch SAS EG')

        self.splitter1.addWidget(self.cb1)
        self.splitter1.addWidget(self.cb2)
        self.splitter1.addWidget(self.cb3)
        self.splitter1.addWidget(self.cb4)
        self.splitter1.addWidget(self.btn)

        # Add list of folders
        self.splitter2 = QSplitter(Qt.Vertical)
        self.bottomleft_layout = QHBoxLayout()
        self.GroupBox1 = QGroupBox("")

        self.table = QTableWidget()
        self.table.verticalHeader().setVisible(False)
        self.table.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)  # close vertical scroll bar
        self.table.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) # close horizontal scroll bar
        self.table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        self.table.verticalHeader().setSectionResizeMode(QHeaderView.Stretch)

        self.hfont = QFont('Times New Roman', 12)
        self.hfont.setBold(True)
        self.table.horizontalHeader().setFont(self.hfont)
        style = "::section {""background-color: lightgray; }"
        self.table.horizontalHeader().setStyleSheet(style)
        self.table.horizontalHeader().setFixedHeight(50)

        self.bottomleft_layout.addWidget(self.table)
        self.GroupBox1.setLayout(self.bottomleft_layout)

        # Add Autoexec
        self.GroupBox2 = QGroupBox("")
        self.bottomright_layout = QVBoxLayout()
        self.textEdit = QTextEdit()

        self.bottomright_layout.addWidget(self.textEdit)
        self.GroupBox2.setLayout(self.bottomright_layout)

        self.tabs = QTabWidget()
        self.tabs.addTab(self.GroupBox1, 'Hyperlinks to Sub Folders')
        self.tabs.addTab(self.GroupBox2, 'Autoexec')

        self.splitter2.addWidget(self.tabs)

        self.mainLayout.addWidget(self.splitter1)
        self.mainLayout.addWidget(self.splitter2)

        self.setLayout(self.mainLayout)


if __name__ == '__main__':
    main()

Here is the warning message. enter image description here

If I remove self.cb1.addItems(['ar-dev', 'ar-prod']) and do not populate first combo box, the error will not occur.


Solution

  • You need sys.coinit_flags = 2 before importing pywinauto. Like it is shown here: https://github.com/pywinauto/py_inspect/blob/master/py_inspect.py