我正在尝试使它位于QMainWindow中,并且可以单击更新。
当前,我能够更新图形,但是我没有工具栏,也没有多个工具栏(仅最新的一种有效)。我希望能够重新绘制图形,并且只有一个工具栏才能正常工作。
在生成20个图形之后,我还会收到一条警告,警告:除非正确关闭它,否则它将占用大量内存,我是否没有以正确的方式删除它?
其工作顺序为:
-我很累的事情:
UI代码
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="0" colspan="2">
<widget class="QWidget" name="content_plot" native="true"/>
</item>
<item row="0" column="1">
<widget class="QPushButton" name="button_process">
<property name="text">
<string>Process</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>30</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
Py代码
import sys
import numpy as np
from PyQt5 import QtCore, QtWidgets, uic
import matplotlib
matplotlib.use('QT5Agg')
import random
import matplotlib.pylab as plt
from matplotlib.backends.backend_qt5agg import FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
global lay
class MyWindow(QtWidgets.QMainWindow):
def update_graph(self, type):
global lay
fig, ax1 = plt.subplots()
if type == 'build':
self.plotWidget = FigureCanvas(fig)
lay = QtWidgets.QVBoxLayout(self.content_plot)
self.nav_tool_bar = self.addToolBar(QtCore.Qt.TopToolBarArea, NavigationToolbar(self.plotWidget, self))
# self.addToolBar(QtCore.Qt.TopToolBarArea, NavigationToolbar(self.plotWidget, self))
else:
print("deleting...?")
lay.removeWidget(self.plotWidget)
self.plotWidget.deleteLater()
self.plotWidget = None
self.removeToolBar(self.nav_tool_bar)
print("updating...")
# test data
x = random.random()
y = random.random()
data = np.array([x, x * 2, x / 2, x * x, x / x, y, y * 2, y * y, y / y, y * x])
bins = np.arange(0.6, 1.62, 0.02)
n1, bins1, patches1 = ax1.hist(data, bins, alpha=0.6, density=False, cumulative=False)
# add toolbar
self.plotWidget = FigureCanvas(fig)
# self.removeToolBar(self.plotWidget, NavigationToolbar(self.plotWidget))
lay.setContentsMargins(0, 0, 0, 0)
lay.addWidget(self.plotWidget)
# nice to have but no way to delete it
# self.addToolBar(QtCore.Qt.TopToolBarArea, NavigationToolbar(self.plotWidget, self))
#else:
# self.removeToolBar(self.plotWidget, self.nav_tool_bar)
# self.removeToolBar(NavigationToolbar(self.plotWidget, self))
# self.removeToolBar(self.plotWidget, NavigationToolbar)
def __init__(self):
super(MyWindow, self).__init__()
uic.loadUi('ui.ui', self)
self.update_graph(type='build')
self.button_process.clicked.connect(lambda:self.update_graph(type='update'))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())