所以我有这个界面,在图像上有两个圆圈叠加。这些圆圈位于两个按钮A和B旁边。默认情况下,圆圈为红色。当用户单击按钮时,圆圈仅将颜色更改为绿色。我在这里找到了使用Chaco在Python中为自己的项目进行操作的代码,但是出于简化目的,我可以使用从此处开始搜索的代码开始。
import sys
import numpy as np
from traits.api import HasTraits, Instance
from traits.api import Bool, Tuple, Float
from traitsui.api import View, Item
from enable.api import Component, ComponentEditor
from chaco.api import Plot, ArrayPlotData
from chaco.api import AbstractOverlay, BaseTool
class CircleTool(AbstractOverlay,BaseTool):
_select_on = Bool(False)
_select_center = Tuple((0,0))
_select_radius = Float(0)
def __init__(self,component=None,**kwargs):
AbstractOverlay.__init__(self,component=component,**kwargs)
BaseTool.__init__(self,component=component,**kwargs)
def center_selection(self,event):
'''
set selection flag to True; save circle center; CircleTool to the
overlays list of component (plot); discard event.
'''
# self._select_on = True
self._select_center = (event.x,event.y)
self.component.overlays.append(self)
event.handled = True
xc, yc = self._select_center
xp, yp = (event.x,event.y)
self._select_radius = float(np.sqrt((xc-xp)**2 + (yc-yp)**2))
self.component.request_redraw()
def overlay(self,component,gc,view_bounds=None,mode='normal'):
gc.set_line_width(2)
gc.set_stroke_color((0,0,0))
gc.clip_to_rect(component.x, component.y, component.width, component.height)
gc.arc(self._select_center[0],self._select_center[1],self._select_radius,0,2*np.pi)
gc.stroke_path()
class CircleOverlay(AbstractOverlay):
'''
basic test overlay: red circle at (200,300) with radius 50.
'''
def overlay(self,component,gc,view_bounds=False,mode='normal'):
gc.set_stroke_color((1,0,0))
gc.set_line_width(2)
gc.arc(200,300,50,0,2*np.pi)
gc.stroke_path()
class PlotExample(HasTraits):
plot = Instance(Component)
traits_view = View(Item('plot',editor=ComponentEditor()),
width=500,height=500,resizable=True,
title='TEST-CIRCLE-TOOL')
def __init__(self,*args,**kwargs):
super(PlotExample,self).__init__(*args,**kwargs)
# create plot
x = np.linspace(0,2*np.pi,100)
y = np.sin(x)
plotData = ArrayPlotData(x=x,y=y)
plot = Plot(plotData)
plot.plot(('x','y'),type='line')
# append circle tool
circleTool = CircleTool(component=plot)
plot.tools.append(circleTool)
# append circle overlay
circleOverlay = CircleOverlay(component=plot)
plot.overlays.append(circleOverlay)
# save plot for later
self.plot = plot
if __name__ == '__main__':
plotExample = PlotExample()
plotExample.configure_traits()
从另一个文件中,用户将按一个QPushbutton,该按钮会说将覆盖颜色更改为绿色,但是我找不到能做到这一点的任何东西。无论如何,我能够做到吗?我曾考虑过创建一个新的叠加层类,但是不确定这是否是最好的方法。