floatcanvas工具栏精确拟合示例在python2中工作,不再在3w/最新wxPython中工作

2024-09-28 15:10:50 发布

您现在位置:Python中文网/ 问答频道 /正文

主要问题是工具栏不再打开。最初的目标是在用户离开/进入窗口/画布时隐藏/显示工具栏,并使用精确匹配应用程序。你知道吗

以下代码与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让它以同样的方式工作,但是我在工具栏上遇到了问题(在第64行也得到了KeyError)。你知道吗

#!/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()

Tags: theimageimportselfsizebinddefevt
1条回答
网友
1楼 · 发布于 2024-09-28 15:10:50

它使用python3.6.7和wxpython4.0.3gtk2工作,但在使用wxpython4.0.1gtk3时失败。[在Linux上]
您未能指定您的操作系统和正在使用的wx版本。
如果我们假设您使用的是4.0.1gtk3,那么答案是将DrawFrame的样式从

wx.Frame.__init__(self, *args,style=wx.FRAME_SHAPED| wx.SIMPLE_BORDER)| wx.FRAME_NO_TASKBAR)

wx.Frame.__init__(self, *args)

还要注意,Canvas.ZoomToBB(margin_adjust=1.2)中的margin_adjust现在是无效的。你知道吗

我还担心您正在尝试导入本地版本的floatcanvas

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

相关问题 更多 >