【问题标题】:react on events generated by chaco tools: how to get values out of a chaco tool when an event is fired ?对 chaco 工具生成的事件做出反应:触发事件时如何从 chaco 工具中获取值?
【发布时间】:2013-03-07 02:11:59
【问题描述】:

实际上这应该是一个非常简单的问题,但我正在经历 chaco 和特质的相当陡峭的学习曲线......

我目前正在编写一个应用程序来使用 chaco 和特征绘制医学图像,我只是想从图像中选择一个像素位置并使用该像素位置对图像堆栈进行评估。所以我开始编写我自己的 Chaco 工具,它可以对图像图上的鼠标点击做出反应。 到目前为止,这工作正常。当我点击 imageplot 时,我可以看到工具内的鼠标坐标(定制的 PixelPickerTool)。但是,由于我想在工具外部使用此坐标值,我的问题是:当触发事件时,如何将坐标移交给工具外部的另一个对象或变量。

为了说明我想要做什么,我附上了我正在编写的两个类的主要结构:

class PixelPickerTool(BaseTool):
    '''Pick a Pixel coordinate from an image'''

    ImageCoordinates = [0,0]

    def normal_left_down(self, event):       
        print "Mouse:", event.x, event.y,
        click_x, click_y = self.component.map_data((event.x, event.y))
        img_x = int(click_x)
        img_y = int(click_y)
        coord = [img_x, img_y]
        if ( (img_x > self.ImageSizeX) or (img_x < 0) ):
             coord = [0,0]
        if ( (img_y > self.ImageSizeY) or (img_y < 0) ):
             coord = [0,0]

        print coord
        # this print gives out the coordinates of the pixel that was clicked - this works fine...
        # so inside the picker too I can get the coordinates
        # but how can I use the coordinates outside this tool ?


class ImagePlot(HasTraits):
    # create simple chaco plot of 2D numpy image array, with a simple interactor (PixelPickerTool) 
    plot = Instance(Plot)
    string = String("hallo")
    picker = Instance(PixelPickerTool)

    traits_view = View(
        Item('plot', editor=ComponentEditor(), show_label=False,width=500, height=500, resizable=False),
        Item('string', show_label=False, springy=True, width=300, height=20, resizable=False),
        title="")   

    def __init__(self, numpyImage):
        super(ImagePlot, self).__init__()
        npImage = np.flipud(np.transpose(numpyImage))

        plotdata = ArrayPlotData(imagedata = npImage)

        plot = Plot(plotdata)
        plot.img_plot("imagedata", colormap=gray)

        self.plot = plot
        # Bild Nullpunkt ist oben links!
        self.plot.default_origin = 'top left'

        pixelPicker = PixelPickerTool(plot)
        self.picker = pixelPicker
        plot.tools.append(pixelPicker)

我想在这个 ImagePlot 类的某处使用由 PixelPickerTool 测量的坐标。例如。通过将它们交给另一个对象,例如 MyImageSeries.setCoordinate(xy_coordinateFromPickerTool) 那么,当触发事件时,如何将像素坐标从 PickerTool 移交给此类中的某个成员变量? 也许是这样的: self.PixelCoordinates = picker.getPixelCoordinates() 可以工作吗? 但是我怎么知道,on_normal_left_down 函数是什么时候在选择器中执行的呢?

最后,我想将坐标交给另一个类,该类保存更多图像以处理图像并在 ImagePlot 中确定的像素位置进行拟合。 我试图在我的 imagePlot 类中使用类似“_picker_changed”的东西来检测是否在 PickerTool 中触发了事件,但这没有检测到事件触发。所以也许我做错了什么......

谁能告诉我如何从这个选择器工具中获取事件和相关变量?

干杯,

安德烈

【问题讨论】:

    标签: traits enthought chaco


    【解决方案1】:

    “但是我怎么知道,on_normal_left_down 函数是什么时候在选取器中执行的呢?”

    您可能有几种方法可以做到这一点,但一种方法是完全按照您的要求进行操作并触发您明确定义的事件。

    例如:

    from traits.api import Event
    
    class PickerTool(BaseTool):
      last_coords = SomeTrait
      i_fired = Event
    
      def normal_left_down(self,event):
         # do whatever necessary processing
         self.last_coords = do_some_stuff(event.some_attribute)
         # now notify your parent
         self.i_fired = True
    

    然后从您要显示的任何位置收听 plot.picker.i_fired,并在 plot.picker.last_coords 中查看保存状态。

    如果您想要对这些坐标执行的操作非常简单,那么您可以做的另一件事可能会更简单,就是传递选择器需要与之交互的数据结构的初始化(或通过一系列对 self 的调用来获取它们.parent) 并直接在选择器中完成您的工作。

    【讨论】:

      猜你喜欢
      • 2010-11-14
      • 2012-08-30
      • 1970-01-01
      • 2019-05-27
      • 2018-11-06
      • 1970-01-01
      • 2012-11-02
      • 2020-06-09
      • 1970-01-01
      相关资源
      最近更新 更多