关闭 VTK 窗口 (Python) [英] Close VTK window (Python)

查看:40
本文介绍了关闭 VTK 窗口 (Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的脚本

导入vtkren = vtk.vtkRenderer()renWin = vtk.vtkRenderWindow()renWin.AddRenderer(ren)iren = vtk.vtkRenderWindowInteractor()iren.SetRenderWindow(renWin)an_actor = vtk.... # 创建一个actorren.AddActor(an_actor)iren.Initialize()renWin.Render()iren.Start()

如果脚本到此结束,一切正常,当手动关闭窗口(单击 X)或按下退出键(Q 或 E)时,将关闭创建的窗口并释放资源.

但是,如果有更多的语句,您会注意到窗口仍然存在,这很好理解,因为我们没有调用任何东西来放弃它,只是结束了交互循环.

通过附加以下内容亲自查看:

temp = raw_input('窗口没有关闭,对吧?(按回车)')

根据VTK/Examples/Cxx/Visualization/CloseWindow,这个

iren.GetRenderWindow().Finalize() # 等效:renWin.Finalize()iren.TerminateApp()

应该完成工作,但没有完成.

我还需要做什么才能以编程方式关闭打开的窗口?

解决方案

简短回答

只缺少一行!

del renWin, iren

长答案

您可能会想制作一个函数来处理这样的窗口关闭

def close_window(iren):render_window = iren.GetRenderWindow()render_window.Finalize()iren.TerminateApp()del render_window, iren

然后使用它(考虑问题中的脚本):

<预><代码>...iren.Initialize()renWin.Render()iren.Start()close_window(iren)

这行不通.原因是

<块引用>

del x 不直接调用 x.__del__() — 前者自减x 的引用计数加一,后者仅在以下情况下调用x 的引用计数达到零(__del__ 文档).

(__del__()iren (vtkRenderWindowInteractor) 和 renWin<上失败(AttributeError)/code> (vtkRenderWindow))

请记住,iren(以及renWin)是在您的脚本中定义的,因此它引用了函数中被删除(假设)的对象.

这会起作用(尽管该函数不会管理所有窗口关闭的东西):

def close_window(iren):render_window = iren.GetRenderWindow()render_window.Finalize()iren.TerminateApp()

然后使用它:

<代码>...iren.Initialize()renWin.Render()iren.Start()close_window(iren)del renWin, 爱尔兰

Consider the following script

import vtk

ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

an_actor = vtk....  # create an actor
ren.AddActor(an_actor)

iren.Initialize()
renWin.Render()
iren.Start()

If the script ends there, everything is alright and the created window will be closed and the resources freed up when the window is closed manually (click X) or an exit key is pressed (Q or E).

However, if there are more statements, you will notice the window is still there, which is quite understandable since we haven't called anything to give it up, just ended the interaction loop.

See for yourself by appending the following:

temp = raw_input('The window did not close, right? (press Enter)')

According to VTK/Examples/Cxx/Visualization/CloseWindow, this

iren.GetRenderWindow().Finalize()  # equivalent: renWin.Finalize()
iren.TerminateApp()

should get the job done but it doesn't.

What else do I have to do to close programatically the opened window?

解决方案

Short answer

Just one line is missing!

del renWin, iren

Long answer

You might be tempted to craft a function to deal with the window closing like this

def close_window(iren):
    render_window = iren.GetRenderWindow()
    render_window.Finalize()
    iren.TerminateApp()
    del render_window, iren

and then use it (consider the script in the question):

...
iren.Initialize()
renWin.Render()
iren.Start()

close_window(iren)

It won't work. The reason is that

del x doesn’t directly call x.__del__() — the former decrements the reference count for x by one, and the latter is only called when x‘s reference count reaches zero (__del__ documentation).

(__del__() fails (AttributeError) on iren (vtkRenderWindowInteractor) and renWin (vtkRenderWindow))

Remember that iren (and renWin too) is defined in your script thus it has a reference to the object being deleted (supposedly) in the function.

This would work (although the function wouldn't manage all the window closing stuff):

def close_window(iren):
    render_window = iren.GetRenderWindow()
    render_window.Finalize()
    iren.TerminateApp()

and then use it:

...
iren.Initialize()
renWin.Render()
iren.Start()

close_window(iren)
del renWin, iren

这篇关于关闭 VTK 窗口 (Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆