Основная проблема в том, что панель инструментов больше не открывается.Первоначальная цель состояла в том, чтобы скрыть / показать панель инструментов, когда пользователь покидает / входит в окно / холст вместе с приложением точной подгонки.
Следующий код работал с FloatCanvas & Python 2:
#!/usr/bin/env python
import wx
# #import the installed version
# from wx.lib.floatcanvas import NavCanvas, FloatCanvas
# import a local version
import sys
sys.path.append("../")
from floatcanvas import NavCanvas, FloatCanvas
# Set a path to an Image file here:
ImageFile = "./white_tank.jpg"
class DrawFrame(wx.Frame):
"""
A frame used for the FloatCanvas Demo
"""
def __init__(self, *args, **kwargs):
# wx.Frame.__init__(self, *args, **kwargs)
wx.Frame.__init__(self, *args,
style=wx.FRAME_SHAPED
| wx.SIMPLE_BORDER
| wx.FRAME_NO_TASKBAR
)
# self.CreateStatusBar()
# Add the Canvas
self.CanvasNav = NavCanvas.NavCanvas(self,
ProjectionFun=None,
style=wx.TRANSPARENT_WINDOW,
# BackgroundColor=(255, 255, 255, 0),
# BackgroundColor="DARK SLATE BLUE",
)
self.CanvasNav.ToolBar.Hide()
Canvas = self.CanvasNav.Canvas
Canvas.MaxScale = 4
self.Canvas = Canvas
FloatCanvas.EVT_MOTION(self.Canvas, self.OnMove)
self.CanvasNav.ZoomButton.Bind(wx.EVT_BUTTON, self.ZoomToFit)
# create the image:
image = wx.Image(ImageFile)
img = Canvas.AddScaledBitmap(image,
(0, 0),
Height=image.GetHeight(),
Position='tl',
Quality='normal',
)
self.image = image
self.set_size(image)
img.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.OnLeftDown)
img.Bind(FloatCanvas.EVT_FC_MOTION, self.OnMotion)
self.Bind(wx.EVT_MOVE, self.OnMove)
self.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeave)
self.Bind(wx.EVT_ENTER_WINDOW, self.OnEnter)
self.Show()
self.CanvasNav.ToolBar.Show()
Canvas.ZoomToBB(margin_adjust=1.2)
self.move_count = 0
def ZoomToFit(self, e):
self.set_size(self.image)
self.Canvas.ZoomToBB(margin_adjust=1.2)
def set_size(self, image):
"""
adjusts the size of the window to fit the aspect ration of the image
"""
# compute the aspect ratio of the image
w, h = image.GetSize()
ar = float(w) / float(h)
# check the size of this window
w, h = self.GetSize()
print w, h
# adjust the width to get the rigth aspect ratio
w = int(h * ar)
self.SetSize( (w, h) )
print "reset size to:"
print self.GetSize()
def OnEnter(self, e):
self.CanvasNav.ToolBar.Show()
self.CanvasNav.ToolBar.Realize()
def OnLeave(self, e):
self.CanvasNav.ToolBar.Hide()
self.CanvasNav.ToolBar.Realize()
def OnMove(self, event):
"""
Updates the status bar with the world coordinates
"""
# self.SetStatusText("%i, %i" % tuple(event.Coords))
def OnLeftDown(self, obj):
print "Left Mouse Clicked on ", obj
def OnMotion(self, obj):
print "mouse moving on image:", self.move_count
self.move_count += 1
app = wx.App(False)
F = DrawFrame(None, title="FloatCanvas Demo App", size=(700, 700))
app.MainLoop()
Вот моя попытка заставить его работать так же, используя последний Python & wxPython, но у меня проблемы с панелью инструментов (также получая KeyError в строке 64).
#!/usr/bin/env python
import wx
# #import the installed version
# from wx.lib.floatcanvas import NavCanvas, FloatCanvas
# import a local version
import sys
sys.path.append("../")
try:
from floatcanvas import NavCanvas, FloatCanvas, Resources
except ImportError: # if it's not there locally, try the wxPython lib.
from wx.lib.floatcanvas import NavCanvas, FloatCanvas, Resources
# Set a path to an Image file here:
# ImageFile = "C:\\Users\\alex\\Downloads\\white_tank.jpg"
ImageFile = "white_tank.jpg"
class DrawFrame(wx.Frame):
"""
A frame used for the FloatCanvas Demo
"""
def __init__(self, *args, **kwargs):
# wx.Frame.__init__(self, *args, **kwargs)
wx.Frame.__init__(self, *args,
style=wx.FRAME_SHAPED
| wx.SIMPLE_BORDER
| wx.FRAME_NO_TASKBAR
)
# self.CreateStatusBar()
# Add the Canvas
self.CanvasNav = NavCanvas.NavCanvas(self,
ProjectionFun=None,
style=wx.TRANSPARENT_WINDOW,
# BackgroundColor=(255, 255, 255, 0),
# BackgroundColor="DARK SLATE BLUE",
)
self.CanvasNav.ToolBar.Hide()
Canvas = self.CanvasNav.Canvas
Canvas.MaxScale = 4
self.Canvas = Canvas
FloatCanvas.EVT_MOTION(self.Canvas, self.OnMove)
self.CanvasNav.ZoomButton.Bind(wx.EVT_BUTTON, self.ZoomToFit)
# create the image:
image = wx.Image(ImageFile)
img = Canvas.AddScaledBitmap(image,
(0, 0),
Height=image.GetHeight(),
Position='tl',
# Quality='normal',
)
self.image = image
self.set_size(image)
img.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.OnLeftDown)
# img.Bind(FloatCanvas.EVT_FC_MOTION, self.OnMotion)
self.Bind(wx.EVT_MOVE, self.OnMove)
self.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeave)
self.Bind(wx.EVT_ENTER_WINDOW, self.OnEnter)
self.Show()
self.CanvasNav.ToolBar.Show()
Canvas.ZoomToBB(margin_adjust=1.2)
self.move_count = 0
def ZoomToFit(self, e):
self.set_size(self.image)
self.Canvas.ZoomToBB(margin_adjust=1.2)
def set_size(self, image):
"""
adjusts the size of the window to fit the aspect ration of the image
"""
# compute the aspect ratio of the image
w, h = image.GetSize()
ar = float(w) / float(h)
# check the size of this window
w, h = self.GetSize()
print(w, h)
# adjust the width to get the rigth aspect ratio
w = int(h * ar)
self.SetSize( (w, h) )
print("reset size to:")
print(self.GetSize())
def OnEnter(self, e):
self.CanvasNav.ToolBar.Show()
self.CanvasNav.ToolBar.Realize()
def OnLeave(self, e):
self.CanvasNav.ToolBar.Hide()
self.CanvasNav.ToolBar.Realize()
def OnMove(self, event):
"""
Updates the status bar with the world coordinates
"""
# self.SetStatusText("%i, %i" % tuple(event.Coords))
def OnLeftDown(self, obj):
print("Left Mouse Clicked on ", obj)
def OnMotion(self, obj):
print("mouse moving on image:", self.move_count)
self.move_count += 1
app = wx.App(False)
F = DrawFrame(None, title="FloatCanvas Demo App", size=(700, 700))
app.MainLoop()