我试图创建一个简单的GUI,其中包含用于查看STL文件的嵌入式mplot3d小部件。我目前正在使用PyQt4和Qt Designer来创建布局和python 3.6.2。我已经使用this post作为起点,并且我已经能够成功实现自定义的mplot3d小部件。但是,我希望能够动态更新mplot3d小部件(例如,单击按钮以加载新的stl文件并更新图形/小部件)。我知道这对于使用
的2D matplotlib数字是可能的manager = matplotlib.get_current_fig_manager()
manager.canvas.draw()
更新常规的matplotlib图。但这对于嵌入式matplotlib小部件是否可行?还是mplot3d自定义小部件?
我应该在QtMplCanvas(FigureCanvas)类中编写新方法吗?我似乎无法从
中找到答案FigureCanvasQTAgg
docs如何从matplotlibs后端更新小部件。我的代码有点长,但我会尝试包含相关位:
from PyQt4 import QtCore, QtGui
import os
import sys
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4 import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
from mpl_toolkits.mplot3d import Axes3D
from stl import mesh
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
plt.switch_backend('Qt4Agg')
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(1007, 607)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.gridLayout = QtGui.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.line = QtGui.QFrame(self.centralwidget)
self.line.setFrameShape(QtGui.QFrame.VLine)
self.line.setFrameShadow(QtGui.QFrame.Sunken)
self.line.setObjectName(_fromUtf8("line"))
self.gridLayout.addWidget(self.line, 2, 1, 1, 1)
# STL VIEWER
self.graphicsView_STL = MPL_WIDGET_3D(self.centralwidget)
self.graphicsView_STL.setObjectName(_fromUtf8("graphicsView_STL"))
self.gridLayout.addWidget(self.graphicsView_STL, 2, 0, 1, 1)
# File load btn
self.import_tlbtn = QtGui.QToolButton(self.centralwidget)
self.import_tlbtn.setObjectName(_fromUtf8("import_tlbtn"))
self.gridLayout.addWidget(self.import_tlbtn, 1, 0, 1, 1)
#... More Widgets, layouts, etc.
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.import_tlbtn.setText(_translate("MainWindow", "...", None))
self.label_slice.setText(_translate("MainWindow", "Slice", None))
self.label_import.setText(_translate("MainWindow", "Import STL File", None))
......来自链接的代码
....用于连接方法的新文件..
from PyQt4 import QtCore, QtGui
import UI_DED
import sys
import os
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from stl import mesh
''' CONNECTING METHODS HERE '''
def open_file():
# Open a new file selection window
widget_f = QtGui.QWidget()
widget_f.resize(320, 240)
widget_f.setWindowTitle("Select a file")
filename = QtGui.QFileDialog.getOpenFileName(widget_f, 'Open File', '/')
if filename == '':
pass
else:
your_mesh = mesh.Mesh.from_file(filename)
stl_update(your_mesh)
# Some sort of method for updating the widget
def stl_update():
pass
# Some method for updating canvas....
if __name__ == "__main__":
plt.switch_backend('Qt4Agg')
# Set window from UI_DED
app = QtGui.QApplication(sys.argv)
MainWindow = UI_DED.QtGui.QMainWindow()
ui = UI_DED.Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
# Connect widgets here
# Import STL File
ui.import_tlbtn.clicked.connect(open_file)
# Exit
sys.exit(app.exec_())