matplotlib 在第二个窗口 pyqt5 [英] matplotlib in second window pyqt5
问题描述
我希望在主窗口中单击按钮时在第二个窗口中绘制 matplotlib 图.使用
QMainWindow
总是建立一个中央小部件,在你的情况下它可以使用 self.main_widget:
class SecondWindow(QMainWindow):def __init__(self):super(SecondWindow, self).__init__()self.main_widget = QtWidgets.QWidget()self.setCentralWidget(self.main_widget)布局 = QtWidgets.QVBoxLayout(self.main_widget)sc = MyMplCanvas(self.main_widget,宽度= 300,高度= 300)layout.addWidget(sc)
I wish to plot a matplotlib graph in a second window when a button is clicked in the main window. Using https://matplotlib.org/examples/user_interfaces/embedding_in_qt4.html , which plots a graph in the main window, I wrote the code below. The matplotlib graph is indeed plotted in a second window, but it has a very small size and I don't understand how to adjust the size of the graph to the window. May I have some help?
from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication, QLabel, QWidget
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from PyQt5 import QtCore, QtWidgets
import numpy as np
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
btn = QPushButton('Click me!', self)
btn.clicked.connect(self.onClick)
def onClick(self):
self.SW = SecondWindow()
self.SW.resize(300,300)
self.SW.show()
class SecondWindow(QMainWindow):
def __init__(self):
super(SecondWindow, self).__init__()
self.main_widget = QtWidgets.QWidget(self)
layout = QtWidgets.QVBoxLayout(self.main_widget)
sc = MyMplCanvas(self.main_widget, width = 300, height = 300)
layout.addWidget(sc)
class MyMplCanvas(FigureCanvas):
def __init__(self, parent=None, width= 300, height= 300):
fig = Figure(figsize=(width, height))
self.axes = fig.add_subplot(111)
self.compute_figure()
FigureCanvas.__init__(self, fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
def compute_figure(self):
t = np.arange(0.0, 3.0, 0.01)
s = np.sin(2*np.pi*t)
self.axes.plot(t, s)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
MW = MainWindow()
MW.resize(500, 500)
MW.show()
sys.exit(app.exec_())
In your case the main_widget does not have the same size as the QMainWindow
so if you change the size with self.SW.resize(300,300)
it will not change the size of self.main_widget, the solution would be to use a layout but in the case of QMainWindow
it is not correct to do so since this has a custom layout that should not be modified:
The solution in the case of QMainWindow
is always to establish a centralwidget, in your case it could be using the self.main_widget:
class SecondWindow(QMainWindow):
def __init__(self):
super(SecondWindow, self).__init__()
self.main_widget = QtWidgets.QWidget()
self.setCentralWidget(self.main_widget)
layout = QtWidgets.QVBoxLayout(self.main_widget)
sc = MyMplCanvas(self.main_widget, width = 300, height = 300)
layout.addWidget(sc)
这篇关于matplotlib 在第二个窗口 pyqt5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!