You need to implement an asynchronous method call after clicking on the QPushButton button. At the moment, after pressing the button, my widget crashes. Tell me, please, what could be the error?

UPD: Fixed some errors, now everything works, I also updated the code listing.

Code Listing:

import asyncio, sys from PyQt5.QtWidgets import QApplication, QWidget, QTextEdit, QPushButton class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setGeometry(300, 300, 400, 220) self.text1 = QTextEdit("Test", self) find = QPushButton("Найти",self) find.move(300, 0) find.clicked.connect(self.Wait) self.show() def Wait(self): task = asyncio.ensure_future(self.set_()) loop = asyncio.get_event_loop() loop.run_until_complete(task) loop.close() @asyncio.coroutine def set_(self): self.text1.setText("1") yield from asyncio.sleep(0) if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) 

The second option:

 import asyncio, sys from PyQt5.QtWidgets import QApplication, QWidget, QTextEdit, QPushButton class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setGeometry(300, 300, 400, 220) self.text1 = QTextEdit("Test", self) find = QPushButton("Найти",self) find.move(300, 0) find.clicked.connect(self.Wait) self.show() def Wait(self): task = asyncio.ensure_future(self.set_()) self.loop = asyncio.get_event_loop() self.loop.run_until_complete(task) self.loop.close() async def set_(self): self.text1.setText("Hello World") await asyncio.sleep(0) if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) 

Closed due to the fact that off-topic participants jfs , PashaPash 27 Dec '16 at 15:15 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - jfs, PashaPash
If the question can be reformulated according to the rules set out in the certificate , edit it .

    0