import sys from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton class MyWindow(QWidget): def __init__(self): super().__init__() self.setWindowTitle("My Window") self.label = QLabel("Hello, PyQt5!", self) self.label.move(50, 50) self.button = QPushButton("Click Me", self) self.button.move(50, 100) self.button.clicked.connect(self.on_button_click) def on_button_click(self): self.label.setText("Button Clicked") if __name__ == "__main__": app = QApplication(sys.argv) window = MyWindow() window.show() sys.exit(app.exec_())
import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QAction class MyWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("My Window") self.setGeometry(100, 100, 300, 300) # create actions self.open_action = QAction("Open", self) self.open_action.triggered.connect(self.on_open) self.save_action = QAction("Save", self) self.save_action.triggered.connect(self.on_save) # create menu self.file_menu = self.menuBar().addMenu("File") self.file_menu.addAction(self.open_action) self.file_menu.addAction(self.save_action) def on_open(self): print("Open") def on_save(self): print("Save") if __name__ == "__main__": app = QApplication(sys.argv) window = MyWindow() window.show() sys.exit(app.exec_())This code creates a window with a menu called "File" and two actions called "Open" and "Save". When the "Open" action is clicked, the `on_open` method is called, which prints "Open" to the console. Similarly, when the "Save" action is clicked, the `on_save` method is called, which prints "Save" to the console.