from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QMessageBox class Example(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle('Example') self.setGeometry(100, 100, 400, 300) button = QPushButton('Click me!', self) button.move(150, 100) button.clicked.connect(self.showMsg) self.show() def showMsg(self): QMessageBox.information(self, 'Message', 'Button clicked!') if __name__ == '__main__': app = QApplication([]) ex = Example() app.exec_()
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton class Example(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle('Example') self.setGeometry(100, 100, 400, 300) button = QPushButton('Click me!', self) button.move(150, 100) button.clicked.connect(self.myFunc) self.show() def myFunc(self): print('Button clicked!') if __name__ == '__main__': app = QApplication([]) ex = Example() app.exec_()This code creates a main window with a button that executes a function when clicked. In this example, the function simply prints a message to the console. These examples demonstrate how to use the QPushButton widget in PyQt5.QtWidgets.