You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

wxPython 图片缩放时固定按钮在指定相对位置的问题求解

问题原因
  • 原有逻辑中updateButtons使用StaticBitmap控件的整体尺寸计算按钮位置,但实际显示的图片是按原始比例缩放的,尺寸和控件尺寸并不一致,导致按钮相对位置偏移,甚至跑到图片显示区域外
  • 固定写死的u=0.3/v=0.7没有对应你初始设置的按钮坐标(250,250),如果要保留初始坐标的相对位置,需要基于原始图片尺寸计算比例
修改方案

你需要调整3处代码:加载图片时记录原始尺寸和初始按钮的相对比例,缩放图片时存储当前实际渲染的图片尺寸,调整按钮位置计算逻辑使用实际图片尺寸。
修改后的完整可运行代码如下:

import wx
import wx.lib.platebtn as pb

class MainFrame(wx.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        self.bInfoSizerVisibility = False

        self.images = []
        # 新增实例变量:原始图片尺寸、按钮相对比例、当前渲染图片尺寸
        self.orig_img_w = 0
        self.orig_img_h = 0
        self.btn_u = 0
        self.btn_v = 0
        self.cur_img_w = 0
        self.cur_img_h = 0
        
        self.initUI()
        self.CenterOnScreen()
        self.Bind(wx.EVT_SIZE, self.OnResizing)

    def initUI(self):
        self.imageSizer = wx.BoxSizer(wx.VERTICAL)
        self.imageSizer.SetMinSize((800, 600))
        self.bitmap = None
        self.image = None
        self.aspect = None

        self.bmpImage = wx.StaticBitmap(self, wx.ID_ANY)
        self.imageSizer.Add(self.bmpImage, 1, wx.EXPAND)

        self.btn = pb.PlateButton(self.bmpImage, -1, 'Click Me!', style=pb.PB_STYLE_NOBG)
        self.btn.Bind(wx.EVT_BUTTON, self.test)
        self.btn.Position = 250, 250

        self.SetSizerAndFit(self.imageSizer)
        self.frameImage()

    def test(self, event):
        print('Button Pressed!')

    def updateButtons(self):
        # 用当前实际渲染的图片尺寸计算按钮位置
        self.btn.Position = int(self.btn_u * self.cur_img_w), int(self.btn_v * self.cur_img_h)

        # 可选:如果需要图片在StaticBitmap中居中显示,用下面的代码替换上面的位置计算
        # bmp_w, bmp_h = self.bmpImage.GetSize()
        # offset_x = (bmp_w - self.cur_img_w) // 2
        # offset_y = (bmp_h - self.cur_img_h) // 2
        # self.btn.Position = offset_x + int(self.btn_u * self.cur_img_w), offset_y + int(self.btn_v * self.cur_img_h)

    def frameImage(self, isJustResize=False):
        if not isJustResize:
            self.bitmap = wx.Bitmap('image.jpg', wx.BITMAP_TYPE_ANY)
            self.image = wx.Bitmap.ConvertToImage(self.bitmap)
            self.aspect = self.image.GetSize()[1] / self.image.GetSize()[0]
            # 加载原始图片时记录原始尺寸和按钮初始相对比例
            self.orig_img_w, self.orig_img_h = self.image.GetSize()
            init_btn_x, init_btn_y = self.btn.Position
            self.btn_u = init_btn_x / self.orig_img_w
            self.btn_v = init_btn_y / self.orig_img_h

        self.Layout()

        sW, sH = self.imageSizer.GetSize()
        newW = sW
        newH = int(newW * self.aspect)

        if newH > sH:
            newH = sH
            newW = int(newH / self.aspect)
        # 存储当前实际渲染的图片尺寸
        self.cur_img_w, self.cur_img_h = newW, newH

        image = self.image.Scale(newW, newH)
        self.bmpImage.SetBitmap(image.ConvertToBitmap())

        self.Layout()
        self.Refresh()
        self.updateButtons()

    def OnResizing(self, event):
        self.frameImage(True)
        event.Skip()

app = wx.App()
frame = MainFrame(None)
frame.Show()
app.MainLoop()

内容的提问来源于stack exchange,提问作者NeoFahrenheit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.08 09:18:04