Python PyCharm关闭后仍显示正在运行的脚本
我今天注意到,在Windows10上运行PyCharm 2020.2 IDE时,当我单击X退出时,主窗口消失,但PyCharm仍显示其运行脚本的图标。在PyCharm中显式终止脚本后,将显示以下错误消息Python PyCharm关闭后仍显示正在运行的脚本,python,qt,pycharm,pyqt5,Python,Qt,Pycharm,Pyqt5,我今天注意到,在Windows10上运行PyCharm 2020.2 IDE时,当我单击X退出时,主窗口消失,但PyCharm仍显示其运行脚本的图标。在PyCharm中显式终止脚本后,将显示以下错误消息 Process finished with exit code -1 在Linux(Ubuntu)上执行my PyCarm IDE中的脚本时,在单击X退出主窗口后,PyCharm不会显示正在运行的脚本的图标 为什么会这样 代码 import sys from datetime import d
Process finished with exit code -1
在Linux(Ubuntu)上执行my PyCarm IDE中的脚本时,在单击X退出主窗口后,PyCharm不会显示正在运行的脚本的图标
为什么会这样
代码
import sys
from datetime import datetime
import pythoncom
import wmi
from PyQt5. QtCore import QObject, QRunnable, QThreadPool, pyqtSignal
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidget, QTableWidgetItem, QHeaderView
class KeyboardDetectorSignals(QObject):
keyboard_changed = pyqtSignal(str)
class KeyboardDetector(QRunnable):
def __init__(self):
super().__init__()
self.signals = KeyboardDetectorSignals()
def run(self):
pythoncom.CoInitialize()
device_connected_wql = "SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA \'Win32_Keyboard\'"
device_disconnected_wql = "SELECT * FROM __InstanceDeletionEvent WITHIN 2 WHERE TargetInstance ISA \'Win32_Keyboard\'"
c = wmi.WMI()
connected_watcher = c.watch_for(raw_wql=device_connected_wql)
disconnected_watcher = c.watch_for(raw_wql=device_disconnected_wql)
while True:
try:
connected = connected_watcher(timeout_ms=10)
except wmi.x_wmi_timed_out:
pass
else:
if connected:
self.signals.keyboard_changed.emit("Keyboard connected.")
try:
disconnected = disconnected_watcher(timeout_ms=10)
except wmi.x_wmi_timed_out:
pass
else:
if disconnected:
self.signals.keyboard_changed.emit("Keyboard disconnected.")
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(100, 100, 500, 500)
self.setWindowTitle("Keyboard Logger")
self.log_table = QTableWidget()
self.log_table.setColumnCount(2)
self.log_table.setShowGrid(True)
self.log_table.setHorizontalHeaderLabels(["Time", "Event"])
self.log_table.horizontalHeader().setStretchLastSection(True)
self.log_table.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)
self.setCentralWidget(self.log_table)
self.show()
self.threadpool = QThreadPool()
keyboard_detector = KeyboardDetector()
keyboard_detector.signals.keyboard_changed.connect(self.add_row)
self.threadpool.start(keyboard_detector)
def add_row(self, event: str):
now = datetime.now()
datetime_string = now.strftime("%Y-%m-%d %H:%M:%S")
row_count = self.log_table.rowCount()
self.log_table.insertRow(row_count)
self.log_table.setItem(row_count, 0, QTableWidgetItem(datetime_string))
self.log_table.setItem(row_count, 1, QTableWidgetItem(event))
def main():
app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
请注意,退出代码-1完成的
过程不是错误,它只是表示您的程序已终止。我不确定,但在代码结束时,您可以尝试编写退出(1)
友好的用户终于解决了此问题
他将代码更改如下:
import sys
from datetime import datetime
import pythoncom
import wmi
from PyQt5. QtCore import QObject, QRunnable, QThreadPool, pyqtSignal
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidget, QTableWidgetItem, QHeaderView
class KeyboardDetectorSignals(QObject):
keyboard_changed = pyqtSignal(str)
class KeyboardDetector(QRunnable):
def __init__(self):
super().__init__()
self._stop = False
self.signals = KeyboardDetectorSignals()
def stop(self):
self._stop = True
def run(self):
pythoncom.CoInitialize()
device_connected_wql = "SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA \'Win32_Keyboard\'"
device_disconnected_wql = "SELECT * FROM __InstanceDeletionEvent WITHIN 2 WHERE TargetInstance ISA \'Win32_Keyboard\'"
c = wmi.WMI()
connected_watcher = c.watch_for(raw_wql=device_connected_wql)
disconnected_watcher = c.watch_for(raw_wql=device_disconnected_wql)
while not self._stop:
try:
connected = connected_watcher(timeout_ms=10)
except wmi.x_wmi_timed_out:
pass
else:
if connected:
self.signals.keyboard_changed.emit("Keyboard connected.")
try:
disconnected = disconnected_watcher(timeout_ms=10)
except wmi.x_wmi_timed_out:
pass
else:
if disconnected:
self.signals.keyboard_changed.emit("Keyboard disconnected.")
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(100, 100, 500, 500)
self.setWindowTitle("Keyboard Logger")
self.log_table = QTableWidget()
self.log_table.setColumnCount(2)
self.log_table.setShowGrid(True)
self.log_table.setHorizontalHeaderLabels(["Time", "Event"])
self.log_table.horizontalHeader().setStretchLastSection(True)
self.log_table.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)
self.setCentralWidget(self.log_table)
self.show()
self.threadpool = QThreadPool()
self.keyboard_detector = KeyboardDetector()
self.keyboard_detector.signals.keyboard_changed.connect(self.add_row)
self.threadpool.start(self.keyboard_detector)
def add_row(self, event: str):
now = datetime.now()
datetime_string = now.strftime("%Y-%m-%d %H:%M:%S")
row_count = self.log_table.rowCount()
self.log_table.insertRow(row_count)
self.log_table.setItem(row_count, 0, QTableWidgetItem(datetime_string))
self.log_table.setItem(row_count, 1, QTableWidgetItem(event))
def closeEvent(self,event):
self.keyboard_detector.stop()
def main():
app = QApplication(sys.argv)
window = MainWindow()
app.exec()
if __name__ == '__main__':
main()
退出代码(0)表示没有错误或问题的退出。退出代码(1)表示存在导致程序退出的问题/问题。这些代码的效果在不同的操作系统之间可能会有所不同,但与Python的效果应该相当一致。据我所知,Python在成功运行时给出的退出代码为0,否则给出的退出代码为1。当其他程序成功时给出0的退出代码,或者如果失败时给出0以外的任何代码时,从命令提示符/终端运行脚本时会发生什么?关闭QT窗口后,脚本是否仍在运行?关闭QT窗口后,脚本将继续在后台运行。关闭窗口后,命令提示符/终端也不接受新命令。最后带有exit(1)
的脚本在关闭Qt窗口后继续在后台运行。