Best Python code snippet using pyatom_python
vtkTkRenderWindowInteractor.py
Source:vtkTkRenderWindowInteractor.py
1"""2A fully functional VTK widget for Tkinter that uses3vtkGenericRenderWindowInteractor. The widget is called4vtkTkRenderWindowInteractor. The initialization part of this code is5similar to that of the vtkTkRenderWidget.6Created by Prabhu Ramachandran, April 20027"""8import Tkinter9import math, os, sys10import vtk11from vtkLoadPythonTkWidgets import vtkLoadPythonTkWidgets12class vtkTkRenderWindowInteractor(Tkinter.Widget):13 """ A vtkTkRenderWidndowInteractor for Python.14 Use GetRenderWindow() to get the vtkRenderWindow.15 Create with the keyword stereo=1 in order to generate a16 stereo-capable window.17 Create with the keyword focus_on_enter=1 to enable18 focus-follows-mouse. The default is for a click-to-focus mode.19 __getattr__ is used to make the widget also behave like a20 vtkGenericRenderWindowInteractor.21 """22 def __init__(self, master, cnf={}, **kw):23 """24 Constructor.25 Keyword arguments:26 rw -- Use passed render window instead of creating a new one.27 stereo -- If True, generate a stereo-capable window.28 Defaults to False.29 focus_on_enter -- If True, use a focus-follows-mouse mode.30 Defaults to False where the widget will use a click-to-focus31 mode.32 """33 # load the necessary extensions into tk34 vtkLoadPythonTkWidgets(master.tk)35 try: # check to see if a render window was specified36 renderWindow = kw['rw']37 except KeyError:38 renderWindow = vtk.vtkRenderWindow()39 try: # was a stereo rendering context requested?40 if kw['stereo']:41 renderWindow.StereoCapableWindowOn()42 del kw['stereo']43 except KeyError:44 pass45 # check if focus should follow mouse46 if kw.get('focus_on_enter'):47 self._FocusOnEnter = 148 del kw['focus_on_enter']49 else:50 self._FocusOnEnter = 051 kw['rw'] = renderWindow.GetAddressAsString("vtkRenderWindow")52 Tkinter.Widget.__init__(self, master, 'vtkTkRenderWidget', cnf, kw)53 self._Iren = vtk.vtkGenericRenderWindowInteractor()54 self._Iren.SetRenderWindow(self._RenderWindow)55 self._Iren.AddObserver('CreateTimerEvent', self.CreateTimer)56 self._Iren.AddObserver('DestroyTimerEvent', self.DestroyTimer)57 self._OldFocus = None58 # private attributes59 self.__InExpose = 060 # create the Tk bindings61 self.BindEvents()62 #self.tk_focusFollowsMouse()63 def __getattr__(self, attr):64 # because the tk part of vtkTkRenderWidget must have65 # the only remaining reference to the RenderWindow when66 # it is destroyed, we can't actually store the RenderWindow67 # as an attribute but instead have to get it from the tk-side68 if attr == '__vtk__':69 return lambda t=self._Iren: t70 elif attr == '_RenderWindow':71 return self.GetRenderWindow()72 elif hasattr(self._Iren, attr):73 return getattr(self._Iren, attr)74 else:75 raise AttributeError, self.__class__.__name__ + \76 " has no attribute named " + attr77 def BindEvents(self):78 """ Bind all the events. """79 self.bind("<Motion>",80 lambda e, s=self: s.MouseMoveEvent(e, 0, 0))81 self.bind("<Control-Motion>",82 lambda e, s=self: s.MouseMoveEvent(e, 1, 0))83 self.bind("<Shift-Motion>",84 lambda e, s=self: s.MouseMoveEvent(e, 1, 1))85 self.bind("<Control-Shift-Motion>",86 lambda e, s=self: s.MouseMoveEvent(e, 0, 1))87 # Left Button88 self.bind("<ButtonPress-1>",89 lambda e, s=self: s.LeftButtonPressEvent(e, 0, 0))90 self.bind("<Control-ButtonPress-1>",91 lambda e, s=self: s.LeftButtonPressEvent(e, 1, 0))92 self.bind("<Shift-ButtonPress-1>",93 lambda e, s=self: s.LeftButtonPressEvent(e, 0, 1))94 self.bind("<Control-Shift-ButtonPress-1>",95 lambda e, s=self: s.LeftButtonPressEvent(e, 1, 1))96 self.bind("<ButtonRelease-1>",97 lambda e, s=self: s.LeftButtonReleaseEvent(e, 0, 0))98 self.bind("<Control-ButtonRelease-1>",99 lambda e, s=self: s.LeftButtonReleaseEvent(e, 1, 0))100 self.bind("<Shift-ButtonRelease-1>",101 lambda e, s=self: s.LeftButtonReleaseEvent(e, 0, 1))102 self.bind("<Control-Shift-ButtonRelease-1>",103 lambda e, s=self: s.LeftButtonReleaseEvent(e, 1, 1))104 # Middle Button105 self.bind("<ButtonPress-2>",106 lambda e, s=self: s.MiddleButtonPressEvent(e, 0, 0))107 self.bind("<Control-ButtonPress-2>",108 lambda e, s=self: s.MiddleButtonPressEvent(e, 1, 0))109 self.bind("<Shift-ButtonPress-2>",110 lambda e, s=self: s.MiddleButtonPressEvent(e, 0, 1))111 self.bind("<Control-Shift-ButtonPress-2>",112 lambda e, s=self: s.MiddleButtonPressEvent(e, 1, 1))113 self.bind("<ButtonRelease-2>",114 lambda e, s=self: s.MiddleButtonReleaseEvent(e, 0, 0))115 self.bind("<Control-ButtonRelease-2>",116 lambda e, s=self: s.MiddleButtonReleaseEvent(e, 1, 0))117 self.bind("<Shift-ButtonRelease-2>",118 lambda e, s=self: s.MiddleButtonReleaseEvent(e, 0, 1))119 self.bind("<Control-Shift-ButtonRelease-2>",120 lambda e, s=self: s.MiddleButtonReleaseEvent(e, 1, 1))121 # Right Button122 self.bind("<ButtonPress-3>",123 lambda e, s=self: s.RightButtonPressEvent(e, 0, 0))124 self.bind("<Control-ButtonPress-3>",125 lambda e, s=self: s.RightButtonPressEvent(e, 1, 0))126 self.bind("<Shift-ButtonPress-3>",127 lambda e, s=self: s.RightButtonPressEvent(e, 0, 1))128 self.bind("<Control-Shift-ButtonPress-3>",129 lambda e, s=self: s.RightButtonPressEvent(e, 1, 1))130 self.bind("<ButtonRelease-3>",131 lambda e, s=self: s.RightButtonReleaseEvent(e, 0, 0))132 self.bind("<Control-ButtonRelease-3>",133 lambda e, s=self: s.RightButtonReleaseEvent(e, 1, 0))134 self.bind("<Shift-ButtonRelease-3>",135 lambda e, s=self: s.RightButtonReleaseEvent(e, 0, 1))136 self.bind("<Control-Shift-ButtonRelease-3>",137 lambda e, s=self: s.RightButtonReleaseEvent(e, 1, 1))138 if sys.platform == 'win32':139 self.bind("<MouseWheel>",140 lambda e, s=self: s.MouseWheelEvent(e, 0, 0))141 self.bind("<Control-MouseWheel>",142 lambda e, s=self: s.MouseWheelEvent(e, 1, 0))143 self.bind("<Shift-MouseWheel>",144 lambda e, s=self: s.MouseWheelEvent(e, 0, 1))145 self.bind("<Control-Shift-MouseWheel>",146 lambda e, s=self: s.MouseWheelEvent(e, 1, 1))147 else:148 # Mouse wheel forward event149 self.bind("<ButtonPress-4>",150 lambda e, s=self: s.MouseWheelForwardEvent(e, 0, 0))151 self.bind("<Control-ButtonPress-4>",152 lambda e, s=self: s.MouseWheelForwardEvent(e, 1, 0))153 self.bind("<Shift-ButtonPress-4>",154 lambda e, s=self: s.MouseWheelForwardEvent(e, 0, 1))155 self.bind("<Control-Shift-ButtonPress-4>",156 lambda e, s=self: s.MouseWheelForwardEvent(e, 1, 1))157 # Mouse wheel backward event158 self.bind("<ButtonPress-5>",159 lambda e, s=self: s.MouseWheelBackwardEvent(e, 0, 0))160 self.bind("<Control-ButtonPress-5>",161 lambda e, s=self: s.MouseWheelBackwardEvent(e, 1, 0))162 self.bind("<Shift-ButtonPress-5>",163 lambda e, s=self: s.MouseWheelBackwardEvent(e, 0, 1))164 self.bind("<Control-Shift-ButtonPress-5>",165 lambda e, s=self: s.MouseWheelBackwardEvent(e, 1, 1))166 # Key related events167 self.bind("<KeyPress>",168 lambda e, s=self: s.KeyPressEvent(e, 0, 0))169 self.bind("<Control-KeyPress>",170 lambda e, s=self: s.KeyPressEvent(e, 1, 0))171 self.bind("<Shift-KeyPress>",172 lambda e, s=self: s.KeyPressEvent(e, 0, 1))173 self.bind("<Control-Shift-KeyPress>",174 lambda e, s=self: s.KeyPressEvent(e, 1, 1))175 self.bind("<KeyRelease>",176 lambda e, s=self: s.KeyReleaseEvent(e, 0, 0))177 self.bind("<Control-KeyRelease>",178 lambda e, s=self: s.KeyReleaseEvent(e, 1, 0))179 self.bind("<Shift-KeyRelease>",180 lambda e, s=self: s.KeyReleaseEvent(e, 0, 1))181 self.bind("<Control-Shift-KeyRelease>",182 lambda e, s=self: s.KeyReleaseEvent(e, 1, 1))183 self.bind("<Enter>",184 lambda e, s=self: s.EnterEvent(e, 0, 0))185 self.bind("<Control-Enter>",186 lambda e, s=self: s.EnterEvent(e, 1, 0))187 self.bind("<Shift-Enter>",188 lambda e, s=self: s.EnterEvent(e, 0, 1))189 self.bind("<Control-Shift-Enter>",190 lambda e, s=self: s.EnterEvent(e, 1, 1))191 self.bind("<Leave>",192 lambda e, s=self: s.LeaveEvent(e, 0, 0))193 self.bind("<Control-Leave>",194 lambda e, s=self: s.LeaveEvent(e, 1, 0))195 self.bind("<Shift-Leave>",196 lambda e, s=self: s.LeaveEvent(e, 0, 1))197 self.bind("<Control-Shift-Leave>",198 lambda e, s=self: s.LeaveEvent(e, 1, 1))199 self.bind("<Configure>", self.ConfigureEvent)200 self.bind("<Expose>",lambda e,s=self: s.ExposeEvent())201 def CreateTimer(self, obj, evt):202 self.after(10, self._Iren.TimerEvent)203 def DestroyTimer(self, obj, event):204 """The timer is a one shot timer so will expire automatically."""205 return 1206 def _GrabFocus(self, enter=0):207 self._OldFocus=self.focus_get()208 self.focus()209 def MouseMoveEvent(self, event, ctrl, shift):210 self._Iren.SetEventInformationFlipY(event.x, event.y, ctrl,211 shift, chr(0), 0, None)212 self._Iren.MouseMoveEvent()213 def LeftButtonPressEvent(self, event, ctrl, shift):214 self._Iren.SetEventInformationFlipY(event.x, event.y, ctrl,215 shift, chr(0), 0, None)216 self._Iren.LeftButtonPressEvent()217 if not self._FocusOnEnter:218 self._GrabFocus()219 def LeftButtonReleaseEvent(self, event, ctrl, shift):220 self._Iren.SetEventInformationFlipY(event.x, event.y, ctrl,221 shift, chr(0), 0, None)222 self._Iren.LeftButtonReleaseEvent()223 def MiddleButtonPressEvent(self, event, ctrl, shift):224 self._Iren.SetEventInformationFlipY(event.x, event.y, ctrl,225 shift, chr(0), 0, None)226 self._Iren.MiddleButtonPressEvent()227 if not self._FocusOnEnter:228 self._GrabFocus()229 def MiddleButtonReleaseEvent(self, event, ctrl, shift):230 self._Iren.SetEventInformationFlipY(event.x, event.y, ctrl,231 shift, chr(0), 0, None)232 self._Iren.MiddleButtonReleaseEvent()233 def RightButtonPressEvent(self, event, ctrl, shift):234 self._Iren.SetEventInformationFlipY(event.x, event.y, ctrl,235 shift, chr(0), 0, None)236 self._Iren.RightButtonPressEvent()237 if not self._FocusOnEnter:238 self._GrabFocus()239 def RightButtonReleaseEvent(self, event, ctrl, shift):240 self._Iren.SetEventInformationFlipY(event.x, event.y, ctrl,241 shift, chr(0), 0, None)242 self._Iren.RightButtonReleaseEvent()243 def MouseWheelEvent(self, event, ctrl, shift):244 self._Iren.SetEventInformationFlipY(event.x, event.y, ctrl,245 shift, chr(0), 0, None)246 if event.delta > 0:247 self._Iren.MouseWheelForwardEvent()248 else:249 self._Iren.MouseWheelBackwardEvent()250 def MouseWheelForwardEvent(self, event, ctrl, shift):251 self._Iren.SetEventInformationFlipY(event.x, event.y, ctrl,252 shift, chr(0), 0, None)253 self._Iren.MouseWheelForwardEvent()254 def MouseWheelBackwardEvent(self, event, ctrl, shift):255 self._Iren.SetEventInformationFlipY(event.x, event.y, ctrl,256 shift, chr(0), 0, None)257 self._Iren.MouseWheelBackwardEvent()258 def KeyPressEvent(self, event, ctrl, shift):259 key = chr(0)260 if event.keysym_num < 256:261 key = chr(event.keysym_num)262 self._Iren.SetEventInformationFlipY(event.x, event.y, ctrl,263 shift, key, 0, event.keysym)264 self._Iren.KeyPressEvent()265 self._Iren.CharEvent()266 def KeyReleaseEvent(self, event, ctrl, shift):267 key = chr(0)268 if event.keysym_num < 256:269 key = chr(event.keysym_num)270 self._Iren.SetEventInformationFlipY(event.x, event.y, ctrl,271 shift, key, 0, event.keysym)272 self._Iren.KeyReleaseEvent()273 def ConfigureEvent(self, event):274 self._Iren.SetSize(event.width, event.height)275 self._Iren.ConfigureEvent()276 def EnterEvent(self, event, ctrl, shift):277 if self._FocusOnEnter:278 self._GrabFocus()279 self._Iren.SetEventInformationFlipY(event.x, event.y, ctrl, shift,280 chr(0), 0, None)281 self._Iren.EnterEvent()282 def LeaveEvent(self, event, ctrl, shift):283 if self._FocusOnEnter and (self._OldFocus != None):284 self._OldFocus.focus()285 self._Iren.SetEventInformationFlipY(event.x, event.y, ctrl, shift,286 chr(0), 0, None)287 self._Iren.LeaveEvent()288 def ExposeEvent(self):289 if (not self.__InExpose):290 self.__InExpose = 1291 if (not self._RenderWindow.IsA('vtkCocoaRenderWindow')):292 self.update()293 self._RenderWindow.Render()294 self.__InExpose = 0295 def GetRenderWindow(self):296 addr = self.tk.call(self._w, 'GetRenderWindow')[5:]297 return vtk.vtkRenderWindow('_%s_vtkRenderWindow_p' % addr)298 def Render(self):299 self._RenderWindow.Render()300#----------------------------------------------------------------------------301def vtkRenderWindowInteractorConeExample():302 """Like it says, just a simple example303 """304 # create root window305 root = Tkinter.Tk()306 # create vtkTkRenderWidget307 pane = vtkTkRenderWindowInteractor(root, width=300, height=300)308 pane.Initialize()309 def quit(obj=root):310 obj.quit()311 pane.AddObserver("ExitEvent", lambda o,e,q=quit: q())312 ren = vtk.vtkRenderer()313 pane.GetRenderWindow().AddRenderer(ren)314 cone = vtk.vtkConeSource()315 cone.SetResolution(8)316 coneMapper = vtk.vtkPolyDataMapper()317 coneMapper.SetInputConnection(cone.GetOutputPort())318 coneActor = vtk.vtkActor()319 coneActor.SetMapper(coneMapper)320 ren.AddActor(coneActor)321 # pack the pane into the tk root322 pane.pack(fill='both', expand=1)323 pane.Start()324 # start the tk mainloop325 root.mainloop()326if __name__ == "__main__":...
vtkTkImageViewerWidget.py
Source:vtkTkImageViewerWidget.py
1"""2A vtkTkImageViewerWidget for python, which is based on the3vtkTkImageWindowWidget.4Specify double=1 to get a double-buffered window.5Created by David Gobbi, Nov 19996"""7import Tkinter8import math, os, sys9import vtk10from vtkLoadPythonTkWidgets import vtkLoadPythonTkWidgets11class vtkTkImageViewerWidget(Tkinter.Widget):12 """13 A vtkTkImageViewerWidget for Python.14 Use GetImageViewer() to get the vtkImageViewer.15 Create with the keyword double=1 in order to generate a16 double-buffered viewer.17 Create with the keyword focus_on_enter=1 to enable18 focus-follows-mouse. The default is for a click-to-focus mode.19 """20 def __init__(self, master, cnf={}, **kw):21 """22 Constructor.23 Keyword arguments:24 iv -- Use passed image viewer instead of creating a new one.25 double -- If True, generate a double-buffered viewer.26 Defaults to False.27 focus_on_enter -- If True, use a focus-follows-mouse mode.28 Defaults to False where the widget will use a click-to-focus29 mode.30 """31 # load the necessary extensions into tk32 vtkLoadPythonTkWidgets(master.tk)33 try: # use specified vtkImageViewer34 imageViewer = kw['iv']35 except KeyError: # or create one if none specified36 imageViewer = vtk.vtkImageViewer()37 doubleBuffer = 038 try:39 if kw['double']:40 doubleBuffer = 141 del kw['double']42 except:43 pass44 # check if focus should follow mouse45 if kw.get('focus_on_enter'):46 self._FocusOnEnter = 147 del kw['focus_on_enter']48 else:49 self._FocusOnEnter = 050 kw['iv'] = imageViewer.GetAddressAsString("vtkImageViewer")51 Tkinter.Widget.__init__(self, master, 'vtkTkImageViewerWidget',52 cnf, kw)53 if doubleBuffer:54 imageViewer.GetRenderWindow().DoubleBufferOn()55 self.BindTkImageViewer()56 def __getattr__(self,attr):57 # because the tk part of vtkTkImageViewerWidget must have58 # the only remaining reference to the ImageViewer when59 # it is destroyed, we can't actually store the ImageViewer60 # as an attribute but instead have to get it from the tk-side61 if attr == '_ImageViewer':62 addr = self.tk.call(self._w, 'GetImageViewer')[5:]63 return vtk.vtkImageViewer('_%s_vtkImageViewer_p' % addr)64 raise AttributeError, self.__class__.__name__ + \65 " has no attribute named " + attr66 def GetImageViewer(self):67 return self._ImageViewer68 def Render(self):69 self._ImageViewer.Render()70 def BindTkImageViewer(self):71 imager = self._ImageViewer.GetRenderer()72 # stuff for window level text.73 mapper = vtk.vtkTextMapper()74 mapper.SetInput("none")75 t_prop = mapper.GetTextProperty()76 t_prop.SetFontFamilyToTimes()77 t_prop.SetFontSize(18)78 t_prop.BoldOn()79 t_prop.ShadowOn()80 self._LevelMapper = mapper81 actor = vtk.vtkActor2D()82 actor.SetMapper(mapper)83 actor.SetLayerNumber(1)84 actor.GetPositionCoordinate().SetValue(4,22)85 actor.GetProperty().SetColor(1,1,0.5)86 actor.SetVisibility(0)87 imager.AddActor2D(actor)88 self._LevelActor = actor89 mapper = vtk.vtkTextMapper()90 mapper.SetInput("none")91 t_prop = mapper.GetTextProperty()92 t_prop.SetFontFamilyToTimes()93 t_prop.SetFontSize(18)94 t_prop.BoldOn()95 t_prop.ShadowOn()96 self._WindowMapper = mapper97 actor = vtk.vtkActor2D()98 actor.SetMapper(mapper)99 actor.SetLayerNumber(1)100 actor.GetPositionCoordinate().SetValue(4,4)101 actor.GetProperty().SetColor(1,1,0.5)102 actor.SetVisibility(0)103 imager.AddActor2D(actor)104 self._WindowActor = actor105 self._LastX = 0106 self._LastY = 0107 self._OldFocus = 0108 self._InExpose = 0109 # bindings110 # window level111 self.bind("<ButtonPress-1>",112 lambda e,s=self: s.StartWindowLevelInteraction(e.x,e.y))113 self.bind("<B1-Motion>",114 lambda e,s=self: s.UpdateWindowLevelInteraction(e.x,e.y))115 self.bind("<ButtonRelease-1>",116 lambda e,s=self: s.EndWindowLevelInteraction())117 # Get the value118 self.bind("<ButtonPress-3>",119 lambda e,s=self: s.StartQueryInteraction(e.x,e.y))120 self.bind("<B3-Motion>",121 lambda e,s=self: s.UpdateQueryInteraction(e.x,e.y))122 self.bind("<ButtonRelease-3>",123 lambda e,s=self: s.EndQueryInteraction())124 self.bind("<Expose>",125 lambda e,s=self: s.ExposeTkImageViewer())126 self.bind("<Enter>",127 lambda e,s=self: s.EnterTkViewer())128 self.bind("<Leave>",129 lambda e,s=self: s.LeaveTkViewer())130 self.bind("<KeyPress-e>",131 lambda e,s=self: s.quit())132 self.bind("<KeyPress-r>",133 lambda e,s=self: s.ResetTkImageViewer())134 def GetImageViewer(self):135 return self._ImageViewer136 def Render(self):137 self._ImageViewer.Render()138 def _GrabFocus(self):139 self._OldFocus=self.focus_get()140 self.focus()141 def EnterTkViewer(self):142 if self._FocusOnEnter:143 self._GrabFocus()144 def LeaveTkViewer(self):145 if self._FocusOnEnter and (self._OldFocus != None):146 self._OldFocus.focus()147 def ExposeTkImageViewer(self):148 if (self._InExpose == 0):149 self._InExpose = 1150 if (not self._ImageViewer.GetRenderWindow().151 IsA('vtkCocoaRenderWindow')):152 self.update()153 self._ImageViewer.Render()154 self._InExpose = 0155 def StartWindowLevelInteraction(self,x,y):156 if not self._FocusOnEnter:157 self._GrabFocus()158 viewer = self._ImageViewer159 self._LastX = x160 self._LastY = y161 self._Window = float(viewer.GetColorWindow())162 self._Level = float(viewer.GetColorLevel())163 # make the window level text visible164 self._LevelActor.SetVisibility(1)165 self._WindowActor.SetVisibility(1)166 self.UpdateWindowLevelInteraction(x,y)167 def EndWindowLevelInteraction(self):168 # make the window level text invisible169 self._LevelActor.SetVisibility(0)170 self._WindowActor.SetVisibility(0)171 self.Render()172 def UpdateWindowLevelInteraction(self,x,y):173 # compute normalized delta174 dx = 4.0*(x - self._LastX)/self.winfo_width()*self._Window175 dy = 4.0*(self._LastY - y)/self.winfo_height()*self._Level176 # abs so that direction does not flip177 if (self._Window < 0.0):178 dx = -dx179 if (self._Level < 0.0):180 dy = -dy181 # compute new window level182 window = self._Window + dx183 if (window < 0.0):184 level = self._Level + dy185 else:186 level = self._Level - dy187 viewer = self._ImageViewer188 viewer.SetColorWindow(window)189 viewer.SetColorLevel(level)190 self._WindowMapper.SetInput("Window: %g" % window)191 self._LevelMapper.SetInput("Level: %g" % level)192 self.Render()193 def ResetTkImageViewer(self):194 # Reset: Set window level to show all values195 viewer = self._ImageViewer196 input = viewer.GetInput()197 if (input == None):198 return199 # Get the extent in viewer200 z = viewer.GetZSlice()201 input.SetUpdateExtent(-99999,99999,-99999,99999,z,z)202 input.Update()203 (low,high) = input.GetScalarRange()204 viewer.SetColorWindow(high - low)205 viewer.SetColorLevel((high + low) * 0.5)206 self.Render()207 def StartQueryInteraction(self,x,y):208 if not self._FocusOnEnter:209 self._GrabFocus()210 # Query PixleValue stuff211 self._WindowActor.SetVisibility(1)212 self.UpdateQueryInteraction(x,y)213 def EndQueryInteraction(self):214 self._WindowActor.SetVisibility(0)215 self.Render()216 def UpdateQueryInteraction(self,x,y):217 viewer = self._ImageViewer218 input = viewer.GetInput()219 z = viewer.GetZSlice()220 # y is flipped upside down221 y = self.winfo_height() - y222 # make sure point is in the extent of the image.223 (xMin,xMax,yMin,yMax,zMin,zMax) = input.GetExtent()224 if (x < xMin or x > xMax or y < yMin or \225 y > yMax or z < zMin or z > zMax):226 return227 numComps = input.GetNumberOfScalarComponents()228 text = ""229 for i in xrange(numComps):230 val = input.GetScalarComponentAsDouble(x,y,z,i)231 text = "%s %.1f" % (text,val)232 self._WindowMapper.SetInput("(%d, %d): %s" % (x,y,text))233 self.Render()234#-----------------------------------------------------------------------------235# an example of how to use this widget236if __name__ == "__main__":237 canvas = vtk.vtkImageCanvasSource2D()238 canvas.SetNumberOfScalarComponents(3)239 canvas.SetScalarType(3)240 canvas.SetExtent(0,511,0,511,0,0)241 canvas.SetDrawColor(100,100,0)242 canvas.FillBox(0,511,0,511)243 canvas.SetDrawColor(200,0,200)244 canvas.FillBox(32,511,100,500)245 canvas.SetDrawColor(100,0,0)246 canvas.FillTube(550,20,30,400,5)247 canvas.SetDrawColor(255,255,255)248 canvas.DrawSegment3D(10,20,0,90,510,0)249 canvas.SetDrawColor(200,50,50)250 canvas.DrawSegment3D(510,90,0,10,20,0)251 # Check segment clipping252 canvas.SetDrawColor(0,200,0)253 canvas.DrawSegment(-10,30,30,-10)254 canvas.DrawSegment(-10,481,30,521)255 canvas.DrawSegment(481,-10,521,30)256 canvas.DrawSegment(481,521,521,481)257 # Check Filling a triangle258 canvas.SetDrawColor(20,200,200)259 canvas.FillTriangle(-100,100,190,150,40,300)260 # Check drawing a circle261 canvas.SetDrawColor(250,250,10)262 canvas.DrawCircle(350,350,200.0)263 # Check drawing a point264 canvas.SetDrawColor(250,250,250)265 canvas.DrawPoint(350,350)266 canvas.DrawPoint(350,550)267 # Test filling functionality268 canvas.SetDrawColor(55,0,0)269 canvas.DrawCircle(450,350,80.0)270 canvas.SetDrawColor(100,255,100)271 canvas.FillPixel(450,350)272 # Create the GUI: two renderer widgets and a quit button273 frame = Tkinter.Frame()274 widget = vtkTkImageViewerWidget(frame,width=512,height=512,double=1)275 viewer = widget.GetImageViewer()276 viewer.SetInputConnection(canvas.GetOutputPort())277 viewer.SetColorWindow(256)278 viewer.SetColorLevel(127.5)279 button = Tkinter.Button(frame,text="Quit",command=frame.quit)280 widget.pack(side='top',padx=3,pady=3,fill='both',expand='t')281 frame.pack(fill='both',expand='t')282 button.pack(fill='x')...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!