Skip to content

How to handle KeyboardInterrupt? Ctrl+C IOT instruction (core dumped). #127

Description

@AI-Maria

I want to shutdown when Ctrl+C is pressed.

from PyQt5.QtWidgets import QMainWindow, QSplitter, QPushButton
from PyQt5.QtCore import Qt
from qasync import QEventLoop, QApplication, asyncClose, asyncSlot
import sys
import asyncio

class MainWindow(QMainWindow):
    def __init__(self, app):
        self.app = app
        super().__init__()
        self.setWindowTitle("MainWindow")
        self.setGeometry(100, 100, 800, 600)
        splitter = QSplitter(Qt.Horizontal)
        self.setCentralWidget(splitter)
        send_button = QPushButton('button')
        splitter.addWidget(send_button)


    @asyncClose
    async def closeEvent(self, event):
        pass

    @asyncSlot()
    async def onMyEvent(self):
        pass

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

    event_loop = QEventLoop(app)
    asyncio.set_event_loop(event_loop)

    app_close_event = asyncio.Event()
    app.aboutToQuit.connect(app_close_event.set)

    main_window = MainWindow(app)
    main_window.show()

    with event_loop:
        event_loop.run_until_complete(app_close_event.wait())

main()

python test.py
Press Ctrl+C and close button.

^CTraceback (most recent call last):
  File "/opt/miniconda3/envs/mar/lib/python3.11/site-packages/qasync/__init__.py", line 766, in wrapper
    @functools.wraps(fn)

KeyboardInterrupt
[1]    3160831 IOT instruction (core dumped)  python src/test.py

pyqt5 5.14.2
qasync 0.27.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions