Is there a way to integrate Pyvista with tkinter?

Viewed 882

I'm working on a python script where it takes a VTI file and plots out the data using pyvista. At its current state, (clicking a button on tkinter), it will run a script and plot my data using pyvista in new window. What I trying to achieve is to display the plot from pyvista in the tkinter winow. Is there a way of doing this?

1 Answers

Yes, but it won't be easy.

First, you need to build VTK with TK support. See the lengthy discussion here for how to do it: https://discourse.vtk.org/t/error-importing-vtktkrenderwindowinteractor-in-vtk-8-2-0/856

Next, there's a bug as of vtk==8.2.0 in vtkLoadPythonTkWidgets.py. Replace all instances of vtkCommonCorePython with vtk in the file.

Once that's done, here's how you can use a pyvista plotter object within tkinter:

import tkinter
import pyvista

from vtk.tk.vtkTkRenderWindowInteractor import vtkTkRenderWindowInteractor


# Setup for root window
root = tkinter.Tk()
root.title("pyvista tk Demo")

frame = tkinter.Frame(root)
frame.pack(fill=tkinter.BOTH, expand=1, side=tkinter.TOP)

# create an instance of a pyvista.Plotter to be used for tk
mesh = pyvista.Sphere()
pl = pyvista.Plotter()
pl.add_mesh(mesh)

# Setup for rendering window interactor
renwininteract = vtkTkRenderWindowInteractor(root, rw=pl.ren_win,
                                             width=400, height=400)
renwininteract.Initialize()
renwininteract.pack(side='top', fill='both', expand=1)
renwininteract.Start()

# Begin execution by updating the renderer and starting the tkinter
# loop
pl.render()
root.mainloop()
Related