PyQt5。 QLayout:尝试将 QLayout“”添加到已经有布局的 PlotWidget“plot_widget”

问题描述 投票:0回答:0

我知道我的问题很老,很容易回答,但我找不到我做错了什么(

from PyQt5.QtWidgets import QMainWindow, QWidget, QVBoxLayout
from controller.mpl_canvas_controller import MplCanvas
from matplotlib.backends.backend_qt5agg import (NavigationToolbar2QT as NavigationToolbar)

class PlotWidget(QWidget):

    def __init__(self, parent) -> None:
       super(PlotWidget, self).__init__(parent)

       self.plot = MplCanvas(self, width=5, height=4, dpi=100)
       self.sub_widget = QMainWindow()
       self.sub_widget.setCentralWidget(self.plot)
       self.sub_widget.addToolBar(NavigationToolbar(self.plot, self.sub_widget))

       layout = QVBoxLayout()
       self.setLayout(layout)
       self.layout().addWidget(self.sub_widget)

为什么我得到 QLayout: Attempting to add QLayout "" to PlotWidget "plot_widget", which already has a layout error??

MplCanvas代码

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
from matplotlib.figure import Figure


class MplCanvas(FigureCanvasQTAgg):

  def __init__(self, parent=None, width=5, height=4, dpi=100):
    fig = Figure(figsize=(width, height), dpi=dpi)  
    self.axes = fig.add_subplot(111)
    super(MplCanvas, self).__init__(fig)
python pyqt pyqt5
© www.soinside.com 2019 - 2024. All rights reserved.