我正在创建一个界面,通过单击按钮在另一个选项卡中绘制图形。我希望这些图形在滚动区域中并排绘制。问题是,当我绘制图形时,图形实际上会缩小以适合小部件,而不是滚动条进行可视化
这里有一个最小的可重复的例子
import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QPushButton,
QLabel, QVBoxLayout, QHBoxLayout, QMessageBox,
QLineEdit,QComboBox, QAction, QTabWidget, QScrollArea)
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
from PyQt5.QtCore import Qt
from PyQt5 import QtGui
class MainWindow(QMainWindow):
#view (GUI)
def __init__(self, parent = None):
#initalizer
super(MainWindow, self).__init__(parent)
#Window
self.setWindowTitle("DataVisualizationPrototype")
self.setGeometry(400, 200, 900, 800)
self.activateWindow()
self.raise_()
self.tab_widget = TabWidget(self)
self.setCentralWidget(self.tab_widget)
class TabWidget(QWidget):
def __init__(self, parent):
super(QWidget, self).__init__(parent)
self.layout =QVBoxLayout(self)
self.tabs = QTabWidget()
self.tab1 = QWidget()
self.tab2 = QWidget()
self.tabs.addTab(self.tab1, "Home")
self.tabs.addTab(self.tab2, "Comparison")
self.layout.addWidget(self.tabs)
self.setLayout(self.layout)
#HOME PAGE
#==========================================
#GeneralLayout
self.tab1layout = QVBoxLayout()
self.tab1.setLayout(self.tab1layout)
self.tab1layout.sizeHint()
plotButton = QPushButton()
self.tab1layout.addWidget(plotButton)
plotbutton.clicked.connect(onclick2)
#COMPARISON TAB
#==========================================
#GeneralLayout
self.tab2layout = QVBoxLayout()
self.tab2.setLayout(self.tab2layout)
self.tab2layout.sizeHint()
self.compGraphLayout = QHBoxLayout()
self.compGraphLayout.addStretch()
self.compScroll = QScrollArea()
self.compScroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
self.compScroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.scrollWidget = QWidget()
self.compScroll.setWidget(self.scrollWidget)
self.compScroll.setWidgetResizable(True)
self.tab2layout.addWidget(self.compScroll)
self.scrollWidget.setLayout(self.compGraphLayout)
def onclick2(self, event):
self.plotLayout = QVBoxLayout()
self.fig = plt.figure()
self.plotLayout.addWidget(FigureCanvas(self.fig))
self.plotLayout.addWidget(NavigationToolbar
(FigureCanvas(self.fig),
self.scrollWidget))
self.compGraphLayout.addLayout(self.plotLayout)
# Plot MRE
self.fig.plt.plot([1, 2, 3, 4])
self.fig.plt.ylabel('some numbers')
FigureCanvas(self.fig).adjustSize()
def main():
pt1 = QApplication(sys.argv)
view = MainWindow()
view.show()
sys.exit(pt1.exec())
if __name__ == '__main__':
main()
暂无答案!
目前还没有任何答案,快来回答吧!