PyQt应用第二次调用错误消息时崩溃问题求助
Hey there, let's tackle this crash issue you're hitting when showing an error message after launching win2 from win1. Given your setup—keeping all logic in main.py and leaving Qt Designer-generated code untouched—here are the most probable fixes and explanations:
A super common cause of crashes here is that your win2 object gets garbage collected mid-operation. If you're creating win2 inside the button click handler as a local variable like this:
def on_win1_button_clicked(self): win2 = Ui_win2() win2.show() # Check inputs and try to show error...
Python will clean up that local win2 variable once the function finishes, even if the error dialog is still open. To fix this, store win2 as an instance variable of your main window class so it sticks around:
def on_win1_button_clicked(self): self.win2 = Ui_win2() # Store as self.win2 instead of local self.win2.show() self.win2.validate_user_input() # Delegate validation to win2's own method
Instead of validating win2's inputs from win1, handle this directly in win2 so it maintains control over its own state. Here's how to structure it:
- In your main.py, extend the win2 UI class to add validation logic (without modifying the Qt-generated code):
from PyQt5.QtWidgets import QMessageBox, QMainWindow from ui_win2 import Ui_win2 # Your Qt Designer-generated UI class class Win2(QMainWindow): def __init__(self, parent=None): super().__init__(parent) self.ui = Ui_win2() self.ui.setupUi(self) # Connect win2's submit button (or whatever triggers validation) to our method self.ui.submit_btn.clicked.connect(self.validate_inputs) def validate_inputs(self): selected_option = self.ui.comboBox.currentText() # Check if user didn't select an option (adjust condition to match your default) if selected_option == "-- Select an Option --": # Use self as the parent for QMessageBox to tie it to win2's context QMessageBox.critical(self, "Validation Error", "Please select an option from the dropdown first!") return # Proceed with your intended logic if input is valid self.process_selected_option(selected_option) def process_selected_option(self, option): # Your code to handle the selected option goes here pass
Then, in win1's button handler, create and show this Win2 instance:
def on_win1_button_clicked(self): self.win2 = Win2(parent=self) # Set win1 as parent for proper window hierarchy self.win2.show()
Using self as the parent for QMessageBox ensures the error dialog is linked to win2, preventing orphaned dialogs that can cause crashes.
Always set a parent for child windows (like win2) when launching them from win1. This helps Qt manage the window hierarchy, prevents garbage collection issues, and ensures dialogs behave correctly:
self.win2 = Win2(parent=self) # Pass win1 as parent
If you don't set a parent, win2 becomes a top-level window, and Qt might not track its lifecycle properly when you show error dialogs.
If the above fixes don't resolve the crash, use PyCharm's debugger to step through your code:
- Set breakpoints right before showing the error message and check if any variables (like
self.ui.comboBox) areNone - Print the state of win2 and the combo box selection with
print(f"Selected option: {selected_option}") - Ensure you've imported all necessary Qt widgets (like
QMessageBox) correctly—missing imports can cause silent crashes
Avoid using sys.exit() or abrupt termination in your error handling; Qt handles window closure gracefully when you use its built-in dialogs properly.
内容的提问来源于stack exchange,提问作者wasystr

