[vtkusers] Call Twice vtkTkRenderWidget in Python

Almar Klein almar.klein at gmail.com
Tue Jan 13 10:25:11 EST 2009


Hello VTKusers,

I had a problem regarding the use of the vtkTkRenderWidget in Python.
After a bit of googling I found more people have had this problem, but
could not find a solution on the web.

I now found the solution myself, and thought to reply to the original poster
(see message below). That post is from december 2004 (thus 4 years old).

Although the VTK version is different (I use VTK 5.21) the problem is the
same.
The problem is cause by the function vtkLoadPythonTkWidgets in module
vtk.tk.vtkLoadPythonTkWidgets. That function has the following lines near
the
top, that prevent the widget to be found a second time:

    # find out if the file is already loaded
    loaded = interp.call('info', 'loaded')
    if string.find(loaded, pkgname) >= 0:
        pass#return

By simply replacing "return" with "pass" the problem is solved.

Cheers,
  Almar


================ MESSAGE BY Alexandre Thinnes =============

I have some problems with management of many vtkTkRenderWidget.

I am developing GUI for surgical software in Python Language.
Each window of this GUI have one vtkTkRenderWidget to show 3D objects
(One of them use the vtkSliceWidget develop by Atamai).
I succeed to show one object into one window.
But if I try to open another one I get this error:

File "/usr/local/lib/python2.2/lib-tk/Tkinter.py", line 1316, in
__call__
return apply(self.func, args)
File "selection.py", line 64, in <lambda>
command = lambda:
File "functionclass.py", line 550, in entranceTriplan
self.show2(self.triplan)
File "functionclass.py", line 164, in show2
self.affichageTriplan("exemple.mnc")
File "functionclass.py", line 584, in affichageTriplan
self.viewer1 =
vtkSliceWidget.vtkSliceWidget(self.triplan.frame1,width=256,height=256)
File "/home/athinnes/atamai/examples/vtkSliceWidget.py", line 108, in
__init__
Tkinter.Widget.__init__(self, master, 'vtkTkRenderWidget', cnf, kw)
File "/usr/local/lib/python2.2/lib-tk/Tkinter.py", line 1780, in
__init__
self.tk.call(
TclError: invalid command name "vtkTkRenderWidget"

This following is a simplify version of the software architecture
without using vtkSliceWidget .
Each module corresponding to one part of the interface ( Here there are
4 modules ):

######################################################################
#testfunc.py

from Tkinter import *
from vtkTkRenderWidget import *
import triplantest
import bouton
import render

class Functions:

def __init__(self, myParent):
print "functions class created"
# If you comment the self.render line. The button will open the
triplantest window
self.render = render.Render(self, myParent)

self.bouton = bouton.Bouton(self)
self.triplan = triplantest.Triplan(self)

## Procedure: show
# this procedure is call to show on the screen the windows winname
def show(self, winname):
winname.deiconify()
self.affichageTriplan()

## Procedure: affichageTriplan
# This procedure setup the slice image inside triplan interface

def affichageTriplan(self):

self.renderWidget = vtkTkRenderWidget(self.triplan.frame1)
self.renderWidget.pack(expand='true',fill='both')


if __name__ == '__main__':

root = Tk()
function = Functions(root)
function.triplan.withdraw()
root.mainloop()

#######################################################################
#render.py

from vtkTkRenderWidget import *
from vtkpython import *
from Tkinter import *

class Render:

def __init__(self, myFunctions, myParentFunction):

self.functions = myFunctions
myParentFunction.title("test vtkTkRenderWidget")

self.viewer = Frame(myParentFunction, borderwidth = 2, relief =
"ridge")
self.renderWidget = vtkTkRenderWidget(self.viewer)

self.viewer.pack(expand='true',fill='both')
self.renderWidget.pack(expand='true',fill='both')

##########################################################################

#triplantest.py

from Tkinter import *

class Triplan(Tk):
def __init__(self, myFct):
Tk.__init__(self)

self.functions = myFct
self.title("Frame for vtkSliceWidget")

self.mainframe = Frame(self)
self.frame1 = Frame(self.mainframe, relief="ridge", borderwidth=2)

self.frame1.pack(expand='true',fill='both')
self.mainframe.pack(expand='true',fill='both')

###############################################################
#bouton.py

from Tkinter import *
class Bouton(Tk):

def __init__(self, myFunctions):
Tk.__init__(self)
self.functions = myFunctions
self.title("Click On Button")
self.frame = Frame(self)
self.bouton = Button(self.frame, text = "click \nhere",
command = lambda:
self.functions.show(self.functions.triplan)
)

self.frame.pack(expand='true',fill='both')
self.bouton.pack(expand='true',fill='both')

######################################################################3

If you run "python testfunc.py" you will see the error. It's the same I
have in the full software.

Somebody have any idea why it's not working and how to solve it?
It seams, vtkTkRenderWidget cannot be call twice. But I don't know how
to solve it.

Thank you for your attention
Best regards
Alexandre Thinnes
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20090113/a5781725/attachment.htm>


More information about the vtkusers mailing list