Я не хочу, чтобы были разрешены дубликаты каких-либо окон (т. е. в приведенном ниже примере кода пользователь может иметь только одно file_tree_window, одно data_plot_window и/или одно cell_monitor_window).
Так, например, если пользователь один раз выбирает «Новый эксперимент», создаются два окна. Если он/она затем снова выберет «Новый эксперимент», эти два исходных окна должны быть закрыты и открыты два новых. То же самое справедливо и для любого из «Модулей анализа». (т. е. выбор уже открытого приведет к закрытию открытого и открытию нового).
Однако у меня возникли проблемы с созданием этой функции.
Во-первых, код:
Код: Выделить всё
class MainWindow(QtGui.QMainWindow):
def __init__(self, model):
super().__init__()
self.resize(1400, 800)
self.model = model
menubar = self.menuBar()
new_experiment_action = QtGui.QAction("New Experiment", self)
new_experiment_action.triggered.connect(self.setup_new_experiment)
file_menu = menubar.addMenu("File")
file_menu.addAction(new_experiment_action)
cell_health_action = QtGui.QAction("Cell Health Monitor", self)
cell_health_action.triggered.connect(self.setup_cell_health_window)
analysis_menu = menubar.addMenu("Analysis Modules")
analysis_menu.addAction(cell_health_action)
self.file_tree = None
self.file_tree_window = None
self.data_plot = None
self.data_plot_window = None
self.cell_health = None
self.cell_health_window = None
self.mdi = QtGui.QMdiArea()
self.setCentralWidget(self.mdi)
def setup_new_experiment(self):
self.mdi.closeAllSubWindows()
self.file_tree = FileTree(self.model)
self.file_tree.setMinimumSize(QtCore.QSize(200, 300))
self.data_plot = DataPlot(self.model)
self.data_plot.setMinimumSize(self.size()*.4)
self.file_tree_window = self.mdi.addSubWindow(self.file_tree)
self.file_tree_window.show()
self.data_plot_window = self.mdi.addSubWindow(self.data_plot)
self.data_plot_window.show()
def setup_cell_health_window(self):
if self.cell_health_window:
self.mdi.removeSubWindow(self.cell_health_window)
self.cell_health = CellHealthMonitor(self.model)
self.cell_health.setMinimumSize(self.size()*.3)
self.cell_health_window = self.mdi.addSubWindow(self.cell_health)
self.cell_health_window.show()
- Если я откройте окно «Cell Health Monitor», закройте его и попытайтесь
открыть его снова, Python выйдет из строя. - Если у меня открыто окно «Cell Health Monitor», и затем попытайтесь начать «Новый эксперимент», после чего откройте окно «Монитор работоспособности ячейки», Python выйдет из строя.
т.е.:
Код: Выделить всё
def setup_new_experiment(self):
for window in self.mdi.subWindowList():
self.mdi.removeSubWindow(window)
self.file_tree = FileTree(self.model)
self.file_tree.setMinimumSize(QtCore.QSize(200, 300))
self.data_plot = DataPlot(self.model)
self.data_plot.setMinimumSize(self.size()*.4)
self.file_tree_window = self.mdi.addSubWindow(self.file_tree)
self.file_tree_window.show()
self.data_plot_window = self.mdi.addSubWindow(self.data_plot)
self.data_plot_window.show()
def setup_cell_health_window(self):
if self.cell_health_window:
self.mdi.removeSubWindow(self.cell_health_window)
self.cell_health = CellHealthMonitor(self.model)
self.cell_health.setMinimumSize(self.size()*.3)
self.cell_health_window = self.mdi.addSubWindow(self.cell_health)
self.cell_health_window.show()
(на самом деле это просто предупреждение):
Код: Выделить всё
QMdiArea::removeSubWindow: window is not inside workspace
Код: Выделить всё
Traceback (most recent call last):
File "MainWindow.py", line 61, in setup_cell_health_window
self.mdi.removeSubWindow(self.cell_health_window)
RuntimeError: wrapped C/C++ object of type QMdiSubWindow has been deleted
Спасибо
Подробнее здесь: https://stackoverflow.com/questions/280 ... -subwindow