PyQt5 图 canvas 不拉伸
PyQt5 figure canvas doesnt stretch
问题是我的 FigureCanvas 没有采用所有可能的高度,它只是调整其宽度。调整 main window 后出现问题。我想让 canvas 占用最大可用高度。
这是现在的样子
这是最小的可重现示例:
import sys
import os
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtGui import (QPixmap, QPainter, QBrush, QPen, QColor)
from PyQt5.QtCore import *
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from PyQt5.QtWidgets import *
import matplotlib
from matplotlib import pyplot as plt
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Distributions")
self.setMinimumSize(480, 320)
# Layout
mainLayout = QVBoxLayout()
dirLayout = QHBoxLayout()
radioLayout = QHBoxLayout()
canvasLayout = QVBoxLayout()
# Setting main Widget
self.mainWidget = QWidget()
self.setCentralWidget(self.mainWidget)
self.mainWidget.setLayout(mainLayout)
# Setting canvas to plot
mainLayout.addLayout(canvasLayout)
figure = plt.figure()
canvas = FigureCanvas(figure)
toolbar = NavigationToolbar(canvas, self)
canvasLayout.addWidget(toolbar)
canvasLayout.addWidget(canvas)
plotButton = QPushButton('Plot')
canvasLayout.addWidget(plotButton,
alignment=QtCore.Qt.AlignCenter)
plotButton.setMaximumSize(QSize(80, 60))
app = QApplication(sys.argv)
dialogi = MainWindow()
dialogi.show()
sys.exit(app.exec_())
将小部件添加到布局时必须设置拉伸因子:
canvasLayout.addWidget(canvas, stretch=1)
问题是我的 FigureCanvas 没有采用所有可能的高度,它只是调整其宽度。调整 main window 后出现问题。我想让 canvas 占用最大可用高度。
这是现在的样子
这是最小的可重现示例:
import sys
import os
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtGui import (QPixmap, QPainter, QBrush, QPen, QColor)
from PyQt5.QtCore import *
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from PyQt5.QtWidgets import *
import matplotlib
from matplotlib import pyplot as plt
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Distributions")
self.setMinimumSize(480, 320)
# Layout
mainLayout = QVBoxLayout()
dirLayout = QHBoxLayout()
radioLayout = QHBoxLayout()
canvasLayout = QVBoxLayout()
# Setting main Widget
self.mainWidget = QWidget()
self.setCentralWidget(self.mainWidget)
self.mainWidget.setLayout(mainLayout)
# Setting canvas to plot
mainLayout.addLayout(canvasLayout)
figure = plt.figure()
canvas = FigureCanvas(figure)
toolbar = NavigationToolbar(canvas, self)
canvasLayout.addWidget(toolbar)
canvasLayout.addWidget(canvas)
plotButton = QPushButton('Plot')
canvasLayout.addWidget(plotButton,
alignment=QtCore.Qt.AlignCenter)
plotButton.setMaximumSize(QSize(80, 60))
app = QApplication(sys.argv)
dialogi = MainWindow()
dialogi.show()
sys.exit(app.exec_())
将小部件添加到布局时必须设置拉伸因子:
canvasLayout.addWidget(canvas, stretch=1)