我一直致力于可视化使用 mayavi.mlab 生成的 3D 图,然后使用 Tkinter 生成 GUI 窗口来改变我的 3D 图的一些参数(准确地说是 2)。我的问题是我无法从 Tkinter 连接画布来托管我的 mayavi 绘图。更具体地说:我为我的 3D 计算创建了一个类和一个用于 GUI 的类。
import numpy as np
from mayavi import mlab
from Tkinter import *
import tkMessageBox as msg
class 3Dplot_calc:
x,y,z = np.mgrid[-10:10:150j,-10:10:150j,-10:10:150j]
def __init__(self, R, I):
self.R = R
self.I = I
中间有一系列方法可以帮助计算有用的部分
class GUI:
def __init__(self,master):
frame = Frame(master)
frame.pack()
self.Set_Figure(frame)
self.Inputs(frame)
def Set_Figure(self,frame):
self.fig = mlab.figure(1, size=(500,500))
### i need to attach it to the canvas somehow and make it upgrade
这就是我卡住的地方!!!GUI 类继续为 R 和 I 定义滑块、一些其他按钮、一个绘图按钮并定义它们在框架中的位置。相关部分是:
def Inputs(self,frame):
input_frame = Frame(frame)
input_frame.grid(column=0, row=0)
#Add Sliders
self.slR = Scale(input_frame, from_=1.0, to=5.0, orient=HORIZONTAL)
self.slR.set(1.0)
self.slI = Scale(input_frame, from_=-5.0, to=5.0, orient=HORIZONTAL)
self.slI.set(1.0)
#Add Plot Button
self.plot_button = Button(input_frame, text='PLOT', command = self.Generate_Values)
def Generate_Values(self):
R = int(self.slR.get())
I = float(self.slI.get())
a = 3Dplot_calc(R,I)
Bx,By,Bz = a.Bx, a.By, a.Bz #Those are the useful methods
field = mlab.pipeline.vector_field(Bx, By, Bz)
magnitude = mlab.pipeline.extract_vector_norm(field)
contours = mlab.pipeline.iso_surface(magnitude,contours=3)
field_lines = mlab.pipeline.streamline(magnitude, seedtype='line',
integration_direction='both')
self.canvas.show()
root = Tk()
gui = GUI(root)
root.mainloop()
3Dplot 类本身可以正常工作。我得到的错误是:GUI 实例没有属性“画布”。如果需要,我可以编辑帖子并放置我的完整代码。