import wx

class MyFrame(wx.Frame): def init(self, parent, title): super(MyFrame, self).init(parent, title=title, size=(600, 400)) self.InitUI()

def InitUI(self):
    # 加载背景图片
    self.bg = wx.Image("bg.jpg", wx.BITMAP_TYPE_ANY).ConvertToBitmap()
    wx.StaticBitmap(self, -1, self.bg, (0, 0))

    # 加载小图片b
    self.b = wx.Image("b.jpg", wx.BITMAP_TYPE_ANY).ConvertToBitmap()
    self.bitmap = wx.StaticBitmap(self, -1, self.b, (100, 100))

    # 绑定事件
    self.bitmap.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
    self.bitmap.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
    self.bitmap.Bind(wx.EVT_MOTION, self.OnMotion)
    self.bitmap.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)

    self.Show(True)

def OnLeftDown(self, e):
    self.startPos = e.GetPosition()
    self.rect = self.bitmap.GetScreenRect()
    self.delta = wx.Point(self.rect.x - self.startPos.x, self.rect.y - self.startPos.y)

    # 画虚线框
    self.dc = wx.ClientDC(self)
    self.dc.SetPen(wx.Pen('white', 1, wx.DOT))
    self.dc.SetBrush(wx.Brush(wx.Colour(0, 0, 0, 0), wx.TRANSPARENT))
    self.dc.DrawRectangle(self.rect)

    # 画四个圆
    self.dc.SetPen(wx.Pen('red', 1))
    self.dc.SetBrush(wx.Brush(wx.Colour(0, 0, 0, 0), wx.TRANSPARENT))
    self.dc.DrawCircle(self.rect.x, self.rect.y, 5)
    self.dc.DrawCircle(self.rect.x + self.rect.width, self.rect.y, 5)
    self.dc.DrawCircle(self.rect.x, self.rect.y + self.rect.height, 5)
    self.dc.DrawCircle(self.rect.x + self.rect.width, self.rect.y + self.rect.height, 5)

def OnLeftUp(self, e):
    # 清除虚线框和圆
    self.dc.Clear()
    self.dc = None

def OnMotion(self, e):
    if e.Dragging() and e.LeftIsDown():
        # 拖动虚线框,缩放b
        pos = e.GetPosition()
        dx = pos.x - self.startPos.x
        dy = pos.y - self.startPos.y
        self.newRect = wx.Rect(self.rect.x + dx, self.rect.y + dy, self.rect.width, self.rect.height)
        self.bitmap.SetBitmap(self.b.GetSubBitmap(self.newRect))

    if e.Dragging() and e.RightIsDown():
        # 拖动四个圆,旋转b
        pos = e.GetPosition()
        dx = pos.x - self.startPos.x
        dy = pos.y - self.startPos.y
        cx = self.rect.x + self.rect.width/2
        cy = self.rect.y + self.rect.height/2
        theta = (pos.x-cx)*(self.startPos.y-cy)-(pos.y-cy)*(self.startPos.x-cx)
        theta = theta / ((pos.x-cx)*(self.startPos.x-cx)+(pos.y-cy)*(self.startPos.y-cy))
        theta = theta * 180 / 3.1415926
        self.newBitmap = self.b.Rotate(theta, (self.rect.x + self.rect.width/2, self.rect.y + self.rect.height/2))
        self.bitmap.SetBitmap(self.newBitmap.ConvertToBitmap())

def OnRightDown(self, e):
    # 点击背景,清除虚线框和圆
    self.dc.Clear()
    self.dc = None

if name == 'main': app = wx.App() MyFrame(None, '图片处理') app.MainLoop(

生成一段程序他的使用wxpython功能如下:1、加载一个图片bg为背景再加载一个小一点的图片b。2、点击图片bb四周会出现虚线框3、点击图片bb四个角会出现四个圈4、拖动b四周虚线框b会随着缩小或者放大5、左右或者上下拖动b四个角会出现的圈b会随着旋转6、点击图片b后再点击背景bgb的四个边出现的虚线框和四个角会出现的四个圈会消失7、要有详细的中文注释8、生成的代码可以运行

原文地址: https://www.cveoy.top/t/topic/fsYt 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录