1

I'm trying to create a simple GUI that has an embedded mplot3d widget for viewing STL files. I'm currently using PyQt4 with Qt Designer for creating the layout and python 3.6.2. I've used this post as a starting point and I have been able to successfully implement a custom mplot3d widget. However, I would like to be able to dynamically update the mplot3d widget (eg. Click a button to load a new stl file and update the figure/widget). I know this is possible for 2D matplotlib figures using

manager = matplotlib.get_current_fig_manager()

manager.canvas.draw()

to update a regular matplotlib figure. But is this possible for an embedded matplotlib widget? Or a mplot3d custom widget?

Should I write I new method in the QtMplCanvas(FigureCanvas) class? I can't seem to figure out from the

FigureCanvasQTAgg

docs how to update a widget from matplotlibs backend. My code is somewhat long but I'll try to include the relevant bits:

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))

... Code from Link

.... New file for connecting methods..

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_())    
Jetpack
  • 9
  • 3

0 Answers0