📄 floatcanvas.py
字号:
x = 20
y += dy
color = "LightBlue"
R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
R.Name = color + " Rectangle"
R.Bind(FloatCanvas.EVT_FC_RIGHT_DOWN, self.RectGotHit)
Canvas.AddText("Right Click Fill", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x += dx
color = "Grey"
R = Canvas.AddEllipse((x, y), (w, h),LineWidth = 2,FillColor = color)
R.Name = color +" Ellipse"
R.Bind(FloatCanvas.EVT_FC_RIGHT_DOWN, self.RectGotHit)
Canvas.AddText("Right Click Fill", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x += dx
color = "Brown"
R = Canvas.AddCircle((x+dx/2, y+dy/2), dx/4, LineWidth = 2, FillColor = color)
R.Name = color + " Circle"
R.HitFill = True
R.Bind(FloatCanvas.EVT_FC_LEFT_DCLICK, self.RectGotHit)
Canvas.AddText("Left D-Click Fill", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x = 20
y += dy
color = "Pink"
R = Canvas.AddCircle((x+dx/2, y+dy/2), dx/4, LineWidth = 2,FillColor = color)
R.Name = color + " Circle"
R.Bind(FloatCanvas.EVT_FC_LEFT_UP, self.RectGotHit)
Canvas.AddText("Left Up Fill", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x += dx
color = "White"
R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
R.Name = color + " Rectangle"
R.Bind(FloatCanvas.EVT_FC_MIDDLE_DOWN, self.RectGotHit)
Canvas.AddText("Middle Down", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x += dx
color = "AQUAMARINE"
R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
R.Name = color + " Rectangle"
R.Bind(FloatCanvas.EVT_FC_MIDDLE_UP, self.RectGotHit)
Canvas.AddText("Middle Up", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x = 20
y += dy
color = "CORAL"
R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
R.Name = color + " Rectangle"
R.Bind(FloatCanvas.EVT_FC_MIDDLE_DCLICK, self.RectGotHit)
Canvas.AddText("Middle DoubleClick", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x += dx
color = "CYAN"
R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
R.Name = color + " Rectangle"
R.Bind(FloatCanvas.EVT_FC_RIGHT_UP, self.RectGotHit)
Canvas.AddText("Right Up", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x += dx
color = "LIME GREEN"
R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
R.Name = color + " Rectangle"
R.Bind(FloatCanvas.EVT_FC_RIGHT_DCLICK, self.RectGotHit)
Canvas.AddText("Right Double Click", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x = 20
y += dy
color = "MEDIUM GOLDENROD"
R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
R.Name = color
R.Bind(FloatCanvas.EVT_FC_RIGHT_DOWN, self.RectGotHitRight)
R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHitLeft)
Canvas.AddText("L and R Click", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x += dx
color = "SALMON"
R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
R.Name = color + " Rectangle"
R.Bind(FloatCanvas.EVT_FC_ENTER_OBJECT, self.RectMouseOver)
Canvas.AddText("Mouse Enter", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x += dx
color = "MEDIUM VIOLET RED"
R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
R.Name = color
R.Bind(FloatCanvas.EVT_FC_LEAVE_OBJECT, self.RectMouseLeave)
Canvas.AddText("Mouse Leave", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x = 20
y += dy
color = "SKY BLUE"
R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
R.Name = color
R.Bind(FloatCanvas.EVT_FC_ENTER_OBJECT, self.RectMouseOver)
R.Bind(FloatCanvas.EVT_FC_LEAVE_OBJECT, self.RectMouseLeave)
Canvas.AddText("Enter and Leave", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x += dx
color = "WHEAT"
R = Canvas.AddRectangle((x, y), (w+12, h), LineColor = None, FillColor = color)
R.Name = color
R.Bind(FloatCanvas.EVT_FC_ENTER_OBJECT, self.RectMouseOver)
R.Bind(FloatCanvas.EVT_FC_LEAVE_OBJECT, self.RectMouseLeave)
Canvas.AddText("Mouse Enter&Leave", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x += dx
color = "KHAKI"
R = Canvas.AddRectangle((x-12, y), (w+12, h), LineColor = None, FillColor = color)
R.Name = color
R.Bind(FloatCanvas.EVT_FC_ENTER_OBJECT, self.RectMouseOver)
R.Bind(FloatCanvas.EVT_FC_LEAVE_OBJECT, self.RectMouseLeave)
Canvas.AddText("Mouse Enter&Leave", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x = 20
y += dy
L = Canvas.AddLine(( (x, y), (x+10, y+10), (x+w, y+h) ), LineWidth = 2, LineColor = "Red")
L.Name = "A Line"
L.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHitLeft)
Canvas.AddText("Left Down", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(L.Name, (x, y+h), Size = FontSize, Position = "tl")
x += dx
color = "SEA GREEN"
Points = N.array(( (x, y), (x, y+2.*h/3), (x+w, y+h), (x+w, y+h/2.), (x + 2.*w/3, y+h/2.), (x + 2.*w/3,y) ), N.float_)
R = Canvas.AddPolygon(Points, LineWidth = 2, FillColor = color)
R.Name = color + " Polygon"
R.Bind(FloatCanvas.EVT_FC_RIGHT_DOWN, self.RectGotHitRight)
Canvas.AddText("RIGHT_DOWN", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x += dx
color = "Red"
Points = N.array(( (x, y), (x, y+2.*h/3), (x+w, y+h), (x+w, y+h/2.), (x + 2.*w/3, y+h/2.), (x + 2.*w/3,y) ), N.float_)
R = Canvas.AddPointSet(Points, Diameter = 4, Color = color)
R.Name = "PointSet"
R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.PointSetGotHit)
Canvas.AddText("LEFT_DOWN", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x = 20
y += dy
T = Canvas.AddText("Hit-able Text", (x, y), Size = 15, Color = "Red", Position = 'tl')
T.Name = "Hit-able Text"
T.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHitLeft)
Canvas.AddText("Left Down", (x, y), Size = FontSize, Position = "bl")
x += dx
T = Canvas.AddScaledText("Scaled Text", (x, y), Size = 1./2*h, Color = "Pink", Position = 'bl')
Canvas.AddPointSet( (x, y), Diameter = 3)
T.Name = "Scaled Text"
T.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHitLeft)
Canvas.AddText("Left Down", (x, y), Size = FontSize, Position = "tl")
x += dx
color = "Cyan"
Point = (x + w/2, y)
#Points = N.array(( (x, y), (x, y+2.*h/3), (x+w, y+h), (x+w, y+h/2.), (x + 2.*w/3, y+h/2.), (x + 2.*w/3,y) ), N.float_)
R = Canvas.AddSquarePoint(Point, Size = 8, Color = color)
R.Name = "SquarePoint"
R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHit)
Canvas.AddText("LEFT_DOWN", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y), Size = FontSize, Position = "tl")
self.Canvas.ZoomToBB()
def TestHitTestForeground(self,event=None):
wx.GetApp().Yield(True)
self.UnBindAllMouseEvents()
Canvas = self.Canvas
Canvas.InitAll()
#Add a Hitable rectangle
w, h = 60, 20
dx = 80
dy = 40
x,y = 20, 20
color = "Red"
R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color, InForeground = False)
R.Name = color + "Rectangle"
R.HitFill = True
R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHit)
Canvas.AddText("Left Click Fill", (x, y), Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Position = "tl")
## A set of Rectangles that move together
## NOTE: In a real app, it might be better to create a new
## custom FloatCanvas DrawObject
self.MovingRects = []
WH = (w/2, h/2)
x += dx
color = "LightBlue"
R = Canvas.AddRectangle((x, y), WH, LineWidth = 2, FillColor = color, InForeground = True)
R.HitFill = True
R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectMoveLeft)
L = Canvas.AddText("Left", (x + w/4, y + h/4), Position = "cc", InForeground = True)
self.MovingRects.extend( (R,L) )
x += w/2
R = Canvas.AddRectangle((x, y), WH, LineWidth = 2, FillColor = color, InForeground = True)
R.HitFill = True
R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectMoveRight)
L = Canvas.AddText("Right", (x + w/4, y + h/4), Position = "cc", InForeground = True)
self.MovingRects.extend( (R,L) )
x -= w/2
y += h/2
R = Canvas.AddRectangle((x, y), WH, LineWidth = 2, FillColor = color, InForeground = True)
R.HitFill = True
R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectMoveUp)
L = Canvas.AddText("Up", (x + w/4, y + h/4), Position = "cc", InForeground = True)
self.MovingRects.extend( (R,L) )
x += w/2
R = Canvas.AddRectangle((x, y), WH, LineWidth = 2, FillColor = color, InForeground = True)
R.HitFill = True
R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectMoveDown)
L = Canvas.AddText("Down", (x + w/4, y + h/4), Position = "cc", InForeground = True)
self.MovingRects.extend( (R,L) )
self.Canvas.ZoomToBB()
def RectMoveLeft(self,Object):
self.MoveRects("left")
def RectMoveRight(self,Object):
self.MoveRects("right")
def RectMoveUp(self,Object):
self.MoveRects("up")
def RectMoveDown(self,Object):
self.MoveRects("down")
def MoveRects(self, Dir):
for Object in self.MovingRects:
X,Y = Object.XY
if Dir == "left": X -= 10
elif Dir == "right": X += 10
elif Dir == "up": Y += 10
elif Dir == "down": Y -= 10
Object.SetPoint((X,Y))
self.Canvas.Draw()
def PointSetGotHit(self, Object):
self.Log(Object.Name + "Got Hit\n")
def RectGotHit(self, Object):
self.Log(Object.Name + "Got Hit\n")
def RectGotHitRight(self, Object):
self.Log(Object.Name + "Got Hit With Right\n")
def RectGotHitLeft(self, Object):
self.Log(Object.Name + "Got Hit with Left\n")
def RectMouseOver(self, Object):
self.Log("Mouse entered:" + Object.Name)
def RectMouseLeave(self, Object):
self.Log("Mouse left " + Object.Name)
def TestText(self, event= None):
wx.GetApp().Yield(True)
self.BindAllMouseEvents()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -