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