PyQt5技术问询:点击菜单栏对象时如何实现打开新窗口功能
Hey there! Let's get that menu bar action working to open a new window in your PyQt5 app. I've put together a complete, working version of your code with the feature implemented, plus breakdowns of the key parts so you understand how it all fits.
Complete Working Code
import sys from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QAction, QLabel, QVBoxLayout from PyQt5.QtGui import QIcon, QPixmap from PyQt5 import QtCore # Define the new window class class NewWindow(QWidget): def __init__(self): super().__init__() self.initNewUI() def initNewUI(self): self.setWindowTitle("New Window") self.setGeometry(200, 200, 300, 200) # Add a sample label to the new window label = QLabel("This is your new window!", self) label.setAlignment(QtCore.Qt.AlignCenter) layout = QVBoxLayout() layout.addWidget(label) self.setLayout(layout) class Example(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.left = 10 self.top = 10 self.width = 800 self.height = 600 self.setWindowTitle("Main Project Window") self.setGeometry(self.left, self.top, self.width, self.height) # Set up central widget and layout for the main window central_widget = QWidget() self.setCentralWidget(central_widget) main_layout = QVBoxLayout(central_widget) # Add your image (replace with your actual image path) pixmap = QPixmap("your_image.png") image_label = QLabel() image_label.setPixmap(pixmap.scaled(400, 300, QtCore.Qt.KeepAspectRatio)) main_layout.addWidget(image_label) # Add your two labels label1 = QLabel("First Project Label", self) main_layout.addWidget(label1) label2 = QLabel("Second Project Label", self) main_layout.addWidget(label2) # Create menu bar and menu items menubar = self.menuBar() window_menu = menubar.addMenu("Windows") # Create the "Open New Window" action open_window_action = QAction(QIcon(), "Open New Window", self) open_window_action.setShortcut("Ctrl+N") # Optional keyboard shortcut open_window_action.triggered.connect(self.launch_new_window) window_menu.addAction(open_window_action) # Optional: Add an exit action for convenience exit_action = QAction(QIcon(), "Exit", self) exit_action.setShortcut("Ctrl+Q") exit_action.triggered.connect(self.close) file_menu = menubar.addMenu("File") file_menu.addAction(exit_action) self.show() def launch_new_window(self): # Instantiate and show the new window # Store it as a class attribute to prevent garbage collection self.new_window = NewWindow() self.new_window.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())
Key Explanations
- NewWindow Class: We create a separate class for the new window to keep code clean and modular. You can customize this class to add any widgets (buttons, text fields, etc.) your project needs.
- Menu Action Binding: The
triggered.connect(self.launch_new_window)line links the menu item click to our function that opens the window. This is PyQt's signal-slot mechanism at work. - Preventing Window Disappearance: By storing the new window as
self.new_window(a class attribute), we avoid it being immediately garbage-collected (which would make it close right after opening). If you want multiple independent windows, you can store them in a list instead. - Main Window Setup: I filled in the missing parts of your original
initUImethod to ensure the main window (with image and labels) displays correctly.
Extra Tips
- Replace
"your_image.png"with the actual path to your image file. If you don't need the image, just remove those lines. - To add more menu items that open different windows, repeat the pattern: create a new window class, add a menu action, and bind it to a function that launches that window.
- If you need communication between the main window and new windows, use PyQt's custom signals and slots to pass data back and forth.
内容的提问来源于stack exchange,提问作者A Damian
相关产品推荐
相关产品推荐

