Python 在导入 pywinauto 包时停止工作,即使我没有使用它

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

我尝试使用 'pywinauto' 包并且它有效。但是当我关闭应用程序时,python 会提示一条消息“python 有 停止工作”。我按照 Vasily Ryabov 的建议添加了 sys.coinit_flags = 2,警告消失了。

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()

但是当我试图使代码复杂化时,如果我点击第一个组合框 select 值 '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()

这是警告信息。

如果我删除 self.cb1.addItems(['ar-dev', 'ar-prod']) 并且不填充第一个组合框,则不会发生错误。

您需要 sys.coinit_flags = 2 才能导入 pywinauto。就像这里显示的那样:https://github.com/pywinauto/py_inspect/blob/master/py_inspect.py