Commit aa4e0d02 authored by David Gobbi's avatar David Gobbi

Fix remaining py3k issues with vtk.tk module.

Two major changes: 1) the vtkLoadPythonTkWidgets utility module is now
imported explicitly as a relative module, necessitating absolute_import.
The alternative is to import it as vtk.tk.vtkLoadPythonTkWidgets, which
makes the tk module less mobile, in case someone wanted to incorporate it
or the vtk module itself into a larger package (e.g. what paraview does).

2) I've explicitly added an "if" check for python3 before importing the
tkinter module.  This is done to ensure that an appropriate error is
reported if the module fails to load (i.e. so that it won't complain
about trying to import "tkinter" on Python 2, just because it had a
fallthrough on an attemp to import "Tkinter".
parent c7d7de28
......@@ -7,20 +7,26 @@ from vtk.test import Testing
from vtk.util.misc import vtkGetDataRoot
VTK_DATA_ROOT = vtkGetDataRoot()
import Tkinter
from vtk.tk.vtkTkRenderWindowInteractor import vtkTkRenderWindowInteractor
from vtk.tk.vtkTkRenderWidget import vtkTkRenderWidget
# Tkinter constants.
E = Tkinter.E
W = Tkinter.W
N = Tkinter.N
S = Tkinter.S
HORIZONTAL = Tkinter.HORIZONTAL
RIGHT = Tkinter.RIGHT
NO = Tkinter.NO
NORMAL = Tkinter.NORMAL
DISABLED = Tkinter.DISABLED
if sys.hexversion < 0x03000000:
# for Python2
import Tkinter as tkinter
else:
# for Python3
import tkinter
# tkinter constants.
E = tkinter.E
W = tkinter.W
N = tkinter.N
S = tkinter.S
HORIZONTAL = tkinter.HORIZONTAL
RIGHT = tkinter.RIGHT
NO = tkinter.NO
NORMAL = tkinter.NORMAL
DISABLED = tkinter.DISABLED
class SuperQuadricViewer(Testing.vtkTest):
'''
......@@ -68,7 +74,7 @@ class SuperQuadricViewer(Testing.vtkTest):
scale.config(state=DISABLED, fg='gray')
win.Render()
self.root = Tkinter.Tk()
self.root = tkinter.Tk()
self.root.title("superquadric viewer")
# Define what to do when the user explicitly closes a window.
self.root.protocol("WM_DELETE_WINDOW", OnClosing)
......@@ -80,23 +86,23 @@ class SuperQuadricViewer(Testing.vtkTest):
# Create parameter sliders
#
prs = Tkinter.Scale(self.root, from_=0, to=3.5, res=0.1,
prs = tkinter.Scale(self.root, from_=0, to=3.5, res=0.1,
orient=HORIZONTAL, label="phi roundness")
trs = Tkinter.Scale(self.root, from_=0, to=3.5, res=0.1,
trs = tkinter.Scale(self.root, from_=0, to=3.5, res=0.1,
orient=HORIZONTAL, label="theta roundness")
thicks = Tkinter.Scale(self.root, from_=0.01, to=1.0, res=0.01,
thicks = tkinter.Scale(self.root, from_=0.01, to=1.0, res=0.01,
orient=HORIZONTAL, label="thickness")
# Create check buttons
#
toroid = Tkinter.IntVar()
toroid = tkinter.IntVar()
toroid.set(0)
doTexture = Tkinter.IntVar()
doTexture = tkinter.IntVar()
doTexture.set(0)
rframe = Tkinter.Frame(self.root)
torbut = Tkinter.Checkbutton(rframe, text="Toroid", variable=toroid)
texbut = Tkinter.Checkbutton(rframe, text="Texture", variable=doTexture)
rframe = tkinter.Frame(self.root)
torbut = tkinter.Checkbutton(rframe, text="Toroid", variable=toroid)
texbut = tkinter.Checkbutton(rframe, text="Texture", variable=doTexture)
# Put it all together
#
......@@ -108,7 +114,7 @@ class SuperQuadricViewer(Testing.vtkTest):
thicks.grid(sticky=N+S+E+W, padx=10, ipady=5, row=1, column=1)
prs.grid(sticky=N+E+W+S, padx=10, ipady=5, row = 2, column = 0)
trs.grid(sticky=N+E+W+S, padx=10, ipady=5, row = 2, column = 1)
Tkinter.Pack.propagate(rframe,NO)
tkinter.Pack.propagate(rframe,NO)
prs.set(1.0)
trs.set(0.7)
......
......@@ -8,13 +8,13 @@ from vtk.test import Testing
from vtk.util.misc import vtkGetDataRoot
VTK_DATA_ROOT = vtkGetDataRoot()
try:
if sys.hexversion < 0x03000000:
# for Python2
import Tkinter as tk
import Tkinter as tkinter
from Tkinter import Pack
except ImportError:
else:
# for Python3
import tkinter as tk
import tkinter
from tkinter import Pack
#from vtk.tk.vtkTkRenderWindowInteractor import vtkTkRenderWindowInteractor
......@@ -22,27 +22,27 @@ from vtk.tk.vtkTkRenderWidget import vtkTkRenderWidget
from vtk.tk.vtkTkImageViewerWidget import vtkTkImageViewerWidget
# Tkinter constants.
E = tk.E
W = tk.W
N = tk.N
S = tk.S
HORIZONTAL = tk.HORIZONTAL
VERTICAL = tk.VERTICAL
RIGHT = tk.RIGHT
LEFT = tk.LEFT
TOP = tk.TOP
BOTTOM = tk.BOTTOM
X = tk.X
BOTH = tk.BOTH
NO = tk.NO
YES = tk.YES
NORMAL = tk.NORMAL
DISABLED = tk.DISABLED
TRUE = tk.TRUE
FALSE = tk.FALSE
GROOVE = tk.GROOVE
INSERT = tk.INSERT
END = tk.END
E = tkinter.E
W = tkinter.W
N = tkinter.N
S = tkinter.S
HORIZONTAL = tkinter.HORIZONTAL
VERTICAL = tkinter.VERTICAL
RIGHT = tkinter.RIGHT
LEFT = tkinter.LEFT
TOP = tkinter.TOP
BOTTOM = tkinter.BOTTOM
X = tkinter.X
BOTH = tkinter.BOTH
NO = tkinter.NO
YES = tkinter.YES
NORMAL = tkinter.NORMAL
DISABLED = tkinter.DISABLED
TRUE = tkinter.TRUE
FALSE = tkinter.FALSE
GROOVE = tkinter.GROOVE
INSERT = tkinter.INSERT
END = tkinter.END
class TestTextActor3DViewer(Testing.vtkTest):
'''
......@@ -136,7 +136,7 @@ class TestTextActor3DViewer(Testing.vtkTest):
self.renWin.AddRenderer(ren)
#self.renWin.SetSize(600, 600)
self.root = tk.Tk()
self.root = tkinter.Tk()
self.root.title("TestTextActor3D.py")
# Define what to do when the user explicitly closes a window.
self.root.protocol("WM_DELETE_WINDOW", OnClosing)
......@@ -163,19 +163,19 @@ class TestTextActor3DViewer(Testing.vtkTest):
self.textActors = dict()
scaleLength = 200
controls = tk.Frame(self.root, relief=GROOVE, bd=2)
controls = tkinter.Frame(self.root, relief=GROOVE, bd=2)
controls.pack(
padx=2, pady=2, anchor=N+W, side=LEFT, fill=BOTH, expand=NO)
# Add control of text.
self.entryText = tk.Text(controls, height=1, width=25)
self.entryText = tkinter.Text(controls, height=1, width=25)
self.entryText.insert(INSERT,baseText)
self.entryText.pack(padx=4, pady=4, side=TOP, fill=X, expand=NO)
self.entryText.bind('<Return>',UpdateTextActors)
self.entryText.bind('<FocusOut>',UpdateTextActors)
# Add control of orientation.
self.scaleOrientation = tk.Scale(controls,
self.scaleOrientation = tkinter.Scale(controls,
from_=0, to=360, res=1,
length= scaleLength,
orient=HORIZONTAL,
......@@ -185,7 +185,7 @@ class TestTextActor3DViewer(Testing.vtkTest):
self.scaleOrientation.pack(side=TOP, fill=X, expand=NO)
# Add control of font size.
self.scaleFontSize = tk.Scale(controls,
self.scaleFontSize = tkinter.Scale(controls,
from_=5, to=150, res=1,
length= scaleLength,
orient=HORIZONTAL,
......@@ -195,7 +195,7 @@ class TestTextActor3DViewer(Testing.vtkTest):
self.scaleFontSize.pack(side=TOP, fill=X, expand=NO)
# Add control of scale.
self.scaleScale = tk.Scale(controls,
self.scaleScale = tkinter.Scale(controls,
from_=0, to=100, res=1,
length= scaleLength,
orient=HORIZONTAL,
......@@ -205,7 +205,7 @@ class TestTextActor3DViewer(Testing.vtkTest):
self.scaleScale.pack(side=TOP, fill=X, expand=NO)
# Add control of scale.
self.scaleOpacity = tk.Scale(controls,
self.scaleOpacity = tkinter.Scale(controls,
from_=0, to=1.0, res=0.01,
length= scaleLength,
orient=HORIZONTAL,
......@@ -228,7 +228,7 @@ class TestTextActor3DViewer(Testing.vtkTest):
UpdateTextActors(0)
for actor in self.textActors.itervalues():
for actor in self.textActors.values():
ren.AddActor(actor)
def DoIt(self):
......
import os
import os, sys
import vtk
from vtk.test import Testing
try:
if sys.hexversion < 0x03000000:
# for Python2
import Tkinter as tk
except ImportError:
import Tkinter as tkinter
else:
# for Python3
import tkinter as tk
import tkinter
from vtk.tk.vtkTkRenderWidget import vtkTkRenderWidget
......@@ -20,7 +20,7 @@ class TestTkRenderWidget(Testing.vtkTest):
# the pipeline will be created afresh for each and every test.
# create a dummy Tkinter root window.
root = tk.Tk()
root = tkinter.Tk()
# create a rendering window and renderer
ren = vtk.vtkRenderer()
......
import os
import os, sys
import vtk
from vtk.test import Testing
try:
if sys.hexversion < 0x03000000:
# for Python2
import Tkinter as tk
except ImportError:
import Tkinter as tkinter
from Tkinter import Pack
else:
# for Python3
import tkinter as tk
import tkinter
from tkinter import Pack
from vtk.tk.vtkTkRenderWindowInteractor import vtkTkRenderWindowInteractor
......@@ -19,7 +21,7 @@ class TestTkRenderWindowInteractor(Testing.vtkTest):
# the pipeline will be created afresh for each and every test.
# create a dummy Tkinter root window.
root = tk.Tk()
root = tkinter.Tk()
# create a rendering window and renderer
ren = vtk.vtkRenderer()
......
......@@ -14,13 +14,13 @@ from vtk.test import Testing
from vtk.util.misc import vtkGetDataRoot
VTK_DATA_ROOT = vtkGetDataRoot()
try:
if sys.hexversion < 0x03000000:
# for Python2
import Tkinter as tk
import Tkinter as tkinter
from Tkinter import Pack
except ImportError:
else:
# for Python3
import tkinter as tk
import tkinter
from tkinter import Pack
#from vtk.tk.vtkTkRenderWindowInteractor import vtkTkRenderWindowInteractor
......@@ -28,24 +28,24 @@ from vtk.tk.vtkTkRenderWidget import vtkTkRenderWidget
from vtk.tk.vtkTkImageViewerWidget import vtkTkImageViewerWidget
# Tkinter constants.
E = tk.E
W = tk.W
N = tk.N
S = tk.S
HORIZONTAL = tk.HORIZONTAL
VERTICAL = tk.VERTICAL
RIGHT = tk.RIGHT
LEFT = tk.LEFT
TOP = tk.TOP
BOTTOM = tk.BOTTOM
X = tk.X
BOTH = tk.BOTH
NO = tk.NO
YES = tk.YES
NORMAL = tk.NORMAL
DISABLED = tk.DISABLED
TRUE = tk.TRUE
FALSE = tk.FALSE
E = tkinter.E
W = tkinter.W
N = tkinter.N
S = tkinter.S
HORIZONTAL = tkinter.HORIZONTAL
VERTICAL = tkinter.VERTICAL
RIGHT = tkinter.RIGHT
LEFT = tkinter.LEFT
TOP = tkinter.TOP
BOTTOM = tkinter.BOTTOM
X = tkinter.X
BOTH = tkinter.BOTH
NO = tkinter.NO
YES = tkinter.YES
NORMAL = tkinter.NORMAL
DISABLED = tkinter.DISABLED
TRUE = tkinter.TRUE
FALSE = tkinter.FALSE
# Global values.
CURSOR_X = 20
......@@ -203,17 +203,17 @@ class Cursor3DViewer(Testing.vtkTest):
self.renWin.SetSize(256, 256)
# Create the GUI: two renderer widgets and a quit button.
self.root = tk.Tk()
self.root = tkinter.Tk()
self.root.title("cursor3D")
# Define what to do when the user explicitly closes a window.
self.root.protocol("WM_DELETE_WINDOW", OnClosing)
# Help label, frame and quit button
helpLabel = tk.Label(self.root,
helpLabel = tkinter.Label(self.root,
text=
"MiddleMouse (or shift-LeftMouse) in image viewer to place cursor")
displayFrame = tk.Frame(self.root)
quitButton = tk.Button(self.root, text= "Quit", command=OnClosing)
displayFrame = tkinter.Frame(self.root)
quitButton = tkinter.Button(self.root, text= "Quit", command=OnClosing)
# Pack the GUI.
helpLabel.pack()
......@@ -221,26 +221,26 @@ class Cursor3DViewer(Testing.vtkTest):
quitButton.pack(fill=X)
# Create the viewer widget.
viewerFrame = tk.Frame(displayFrame)
viewerFrame = tkinter.Frame(displayFrame)
viewerFrame.pack(padx=3, pady=3, side=LEFT, anchor=N,
fill=BOTH, expand=FALSE)
self.tkvw = vtkTkImageViewerWidget(viewerFrame, iv=self.viewer,
width=264, height=264)
viewerControls = tk.Frame(viewerFrame)
viewerControls = tkinter.Frame(viewerFrame)
viewerControls.pack(side=BOTTOM, anchor=S, fill=BOTH, expand=TRUE)
self.tkvw.pack(side=TOP, anchor=N, fill=BOTH, expand=FALSE)
downButton = tk.Button(viewerControls, text="Down",
downButton = tkinter.Button(viewerControls, text="Down",
command=[ViewerDown,self.viewer])
upButton = tk.Button(viewerControls, text="Up",
upButton = tkinter.Button(viewerControls, text="Up",
command=[ViewerUp,self.viewer])
sliceLabel = tk.Label(viewerControls,
sliceLabel = tkinter.Label(viewerControls,
text="slice: "+str(CURSOR_Z*IMAGE_MAG_Z))
downButton.pack(side=LEFT, expand=TRUE, fill=BOTH)
upButton.pack(side=LEFT, expand=TRUE, fill=BOTH)
sliceLabel.pack(side=LEFT, expand=TRUE, fill=BOTH)
# Create the render widget
renderFrame = tk.Frame(displayFrame)
renderFrame = tkinter.Frame(displayFrame)
renderFrame.pack(padx=3, pady=3, side=LEFT, anchor=N,
fill=BOTH, expand=TRUE)
self.tkrw = vtkTkRenderWidget(renderFrame, rw=self.renWin,
......
import sys, os, string
import sys, os
import vtkCommonCorePython
def vtkLoadPythonTkWidgets(interp):
......@@ -12,7 +12,7 @@ def vtkLoadPythonTkWidgets(interp):
Y = vtkCommonCorePython.vtkVersion.GetVTKMinorVersion()
modname = 'vtkRenderingPythonTkWidgets'
name = '%s-%d.%d' % (modname,X,Y)
pkgname = string.capitalize(string.lower(modname))
pkgname = modname.lower().capitalize()
# find out if the module is already loaded
loadedpkgs = interp.call('info', 'loaded')
......@@ -40,7 +40,7 @@ def vtkLoadPythonTkWidgets(interp):
pathlist = sys.path
# add tcl paths, ensure that {} is handled properly
try:
auto_paths = string.split(interp.getvar('auto_path'))
auto_paths = interp.getvar('auto_path').split()
except AttributeError:
auto_paths = interp.getvar('auto_path')
for path in auto_paths:
......@@ -58,6 +58,10 @@ def vtkLoadPythonTkWidgets(interp):
if os.name == 'posix':
pathlist.append('/usr/local/lib')
# if python 3, there is no separate "unicode" type
if sys.hexversion >= 0x03000000:
unicode = str
# attempt to load
for path in pathlist:
try:
......
......@@ -7,18 +7,20 @@ Specify double=1 to get a double-buffered window.
Created by David Gobbi, Nov 1999
"""
from __future__ import absolute_import
import math, os, sys
import vtk
try:
if sys.hexversion < 0x03000000:
# for Python2
import Tkinter
except ImportError:
import Tkinter as tkinter
else:
# for Python3
import tkinter
from vtkLoadPythonTkWidgets import vtkLoadPythonTkWidgets
from .vtkLoadPythonTkWidgets import vtkLoadPythonTkWidgets
class vtkTkImageViewerWidget(Tkinter.Widget):
class vtkTkImageViewerWidget(tkinter.Widget):
"""
A vtkTkImageViewerWidget for Python.
......@@ -69,7 +71,7 @@ class vtkTkImageViewerWidget(Tkinter.Widget):
self._FocusOnEnter = 0
kw['iv'] = imageViewer.GetAddressAsString("vtkImageViewer")
Tkinter.Widget.__init__(self, master, 'vtkTkImageViewerWidget',
tkinter.Widget.__init__(self, master, 'vtkTkImageViewerWidget',
cnf, kw)
if doubleBuffer:
imageViewer.GetRenderWindow().DoubleBufferOn()
......@@ -347,7 +349,7 @@ if __name__ == "__main__":
# Create the GUI: two renderer widgets and a quit button
frame = Tkinter.Frame()
frame = tkinter.Frame()
widget = vtkTkImageViewerWidget(frame,width=512,height=512,double=1)
viewer = widget.GetImageViewer()
......@@ -355,7 +357,7 @@ if __name__ == "__main__":
viewer.SetColorWindow(256)
viewer.SetColorLevel(127.5)
button = Tkinter.Button(frame,text="Quit",command=frame.quit)
button = tkinter.Button(frame,text="Quit",command=frame.quit)
widget.pack(side='top',padx=3,pady=3,fill='both',expand='t')
frame.pack(fill='both',expand='t')
......
"""
A subclass of Tkinter.PhotoImage that connects a
A subclass of tkinter.PhotoImage that connects a
vtkImageData to a photo widget.
Created by Daniel Blezek, August 2002
"""
try:
from __future__ import absolute_import
import sys
if sys.hexversion < 0x03000000:
# for Python2
import Tkinter
except ImportError:
import Tkinter as tkinter
else:
# for Python3
import tkinter
from vtkLoadPythonTkWidgets import vtkLoadPythonTkWidgets
from .vtkLoadPythonTkWidgets import vtkLoadPythonTkWidgets
class vtkTkPhotoImage ( Tkinter.PhotoImage ):
class vtkTkPhotoImage ( tkinter.PhotoImage ):
"""
A subclass of PhotoImage with helper functions
for displaying vtkImageData
"""
def __init__ ( self, **kw ):
# Caller the superclass
Tkinter.PhotoImage.__init__ ( self, kw )
tkinter.PhotoImage.__init__ ( self, kw )
vtkLoadPythonTkWidgets ( self.tk )
def PutImageSlice ( self, image, z, orientation='transverse', window=256, level=128 ):
......
"""
A simple vtkTkRenderWidget for Tkinter.
A simple vtkTkRenderWidget for tkinter.
Created by David Gobbi, April 1999
......@@ -14,9 +14,7 @@ Aug 29, 1999 - Renamed file to vtkRenderWidget.py
Nov 14, 1999 - Added support for keyword 'rw'
Mar 23, 2000 - Extensive but backwards compatible changes,
improved documentation
"""
"""
A few important notes:
This class is meant to be used as a base-class widget for
......@@ -44,18 +42,20 @@ i.e. set to some initial value. Don't forget that 'None' means
'NULL' - the python/vtk wrappers guarantee their equivalence.
"""
from __future__ import absolute_import
import math, os, sys
import vtk
try:
if sys.hexversion < 0x03000000:
# for Python2
import Tkinter
except ImportError:
import Tkinter as tkinter
else:
# for Python3
import tkinter
from vtkLoadPythonTkWidgets import vtkLoadPythonTkWidgets
from .vtkLoadPythonTkWidgets import vtkLoadPythonTkWidgets
class vtkTkRenderWidget(Tkinter.Widget):
class vtkTkRenderWidget(tkinter.Widget):
"""
A vtkTkRenderWidget for Python.
......@@ -105,7 +105,7 @@ class vtkTkRenderWidget(Tkinter.Widget):
self._FocusOnEnter = 0
kw['rw'] = renderWindow.GetAddressAsString("vtkRenderWindow")
Tkinter.Widget.__init__(self, master, 'vtkTkRenderWidget', cnf, kw)
tkinter.Widget.__init__(self, master, 'vtkTkRenderWidget', cnf, kw)
self._CurrentRenderer = None
self._CurrentCamera = None
......@@ -443,7 +443,7 @@ def vtkRenderWidgetConeExample():
"""Like it says, just a simple example
"""
# create root window
root = Tkinter.Tk()
root = tkinter.Tk()
# create vtkTkRenderWidget
pane = vtkTkRenderWidget(root,width=300,height=300)
......
"""
A fully functional VTK widget for Tkinter that uses
A fully functional VTK widget for tkinter that uses
vtkGenericRenderWindowInteractor. The widget is called
vtkTkRenderWindowInteractor. The initialization part of this code is
similar to that of the vtkTkRenderWidget.
......@@ -9,19 +9,20 @@ Created by Prabhu Ramachandran, April 2002
"""
from __future__ import absolute_import
import math, os, sys
import vtk
try:
if sys.hexversion < 0x03000000:
# for Python2
import Tkinter
except ImportError:
import Tkinter as tkinter
else:
# for Python3
import tkinter
from .vtkLoadPythonTkWidgets import vtkLoadPythonTkWidgets
from vtkLoadPythonTkWidgets import vtkLoadPythonTkWidgets
class vtkTkRenderWindowInteractor(Tkinter.Widget):
class vtkTkRenderWindowInteractor(tkinter.Widget):
""" A vtkTkRenderWidndowInteractor for Python.
Use GetRenderWindow() to get the vtkRenderWindow.
......@@ -73,7 +74,7 @@ class vtkTkRenderWindowInteractor(Tkinter.Widget):
self._FocusOnEnter = 0
kw['rw'] = renderWindow.GetAddressAsString("vtkRenderWindow")
Tkinter.Widget.__init__(self, master, 'vtkTkRenderWidget', cnf, kw)
tkinter.Widget.__init__(self, master, 'vtkTkRenderWidget', cnf, kw)
self._Iren = vtk.vtkGenericRenderWindowInteractor()
self._Iren.SetRenderWindow(self._RenderWindow)
......@@ -365,7 +366,7 @@ def vtkRenderWindowInteractorConeExample():
"""Like it says, just a simple example
"""
# create root window
root = Tkinter.Tk()
root = tkinter.Tk()
# create vtkTkRenderWidget
pane = vtkTkRenderWindowInteractor(root, width=300, height=300)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment