简体   繁体   English

当 sizer 的尺寸在 wxPython 中改变时动态改变图像尺寸

[英]Changing the image size dynamically when the sizer's size changes in wxPython

I am working with python v2.7 and wxPython v3.0 on Windows 8 OS.我正在 Windows 8 操作系统上使用 python v2.7 和 wxPython v3.0。 In the code snippet provided below simply creates a frame which has a vertical sizer that contains an image and an other horizontal sizer that in turn contains two panels.在下面提供的代码片段中,简单地创建了一个框架,该框架具有一个包含图像的垂直尺寸调整器和另一个包含两个面板的水平尺寸调整器。

Question : When I resize the frame by dragging the border, the panels resize automatically.问题:当我通过拖动边框调整框架大小时,面板会自动调整大小。 Unlike the panels the image doesn't resize?与面板不同,图像不会调整大小? How can I make my image to behave like panels for the case of resizing?在调整大小的情况下,如何使我的图像表现得像面板?

Code: The image file used can be downloaded from here: green.bmp代码:使用的图像文件可以从这里下载: green.bmp

import wx

class Frame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, style=wx.DEFAULT_FRAME_STYLE)
        panelA = wx.Panel(self,-1)
        imageFile = wx.Image('greenbig.bmp', wx.BITMAP_TYPE_ANY).ConvertToBitmap()
        myBitmap = wx.StaticBitmap(panelA, -1, imageFile)

        panel1 = wx.Panel(self, -1,style=wx.SIMPLE_BORDER)
        panel2 = wx.Panel(self, -1, style=wx.SIMPLE_BORDER)
        panelSizer = wx.BoxSizer(wx.HORIZONTAL)
        panelSizer.Add(panel1, 1, wx.ALL|wx.EXPAND, 0)
        panelSizer.Add(panel2, 1, wx.ALL|wx.EXPAND, 0)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(panelA, 1, wx.ALL|wx.EXPAND, 0)
        sizer.Add(panelSizer,  1, wx.ALL|wx.EXPAND, 0)
        self.SetSizer(sizer)

        self.Show(True)

app = wx.App()
frame = Frame(None, wx.ID_ANY, 'Image')
app.MainLoop()

Thank you for your time!感谢您的时间!

(eight years later) I was struggling to show an image that was dynamically resized with the window frame. (八年后)我正在努力展示一张使用 window 帧动态调整大小的图像。 These days, I'm using Python 3 with wxPython version 4 .这些天,我将Python 3wxPython version 4一起使用。 I pieced this solution from a lot of reading of the documentation and tips on how best to use sizers.我通过大量阅读文档和有关如何最好地使用 sizer 的提示拼凑出这个解决方案。 I then whittled my code down to the absolute minimum required to accomplish this narrow use case.然后我将我的代码削减到完成这个狭窄用例所需的绝对最低限度。

Notice that I don't ever call Frame.Layout() or Panel.Layout() because I rely on the initial size of the Frame and the fact that a single Panel in a Frame will be automatically resized with the Frame .请注意,我从未调用Frame.Layout()Panel.Layout() ,因为我依赖于Frame的初始大小以及Frame中的单个Panel将随Frame自动调整大小这一事实。 Also, any call to Fit() was unnecessary.此外,对Fit()的任何调用都是不必要的。 The sizer containing the ImagePanel does need the EXPAND flag, but the ImagePanel does not need a sizer itself.包含ImagePanel的 sizer 确实需要EXPAND标志,但ImagePanel本身不需要 sizer。

Note: you'll have to supply your own example image to load.注意:您必须提供自己的示例图像才能加载。

import wx


class ImagePanel(wx.Panel):
    def __init__(self, parent):
        super().__init__(parent)
        self.img = wx.Image(width=1, height=1)
        self.ctrl = wx.StaticBitmap(self, bitmap=self.img.ConvertToBitmap())
        self.Bind(wx.EVT_SIZE, self.on_resize)

    def load_image(self):
        with open('image.png', 'rb') as fin:
            self.img = wx.Image(fin)
        self.update_bitmap()

    def update_bitmap(self):
        w, h = self.GetSize()
        self.ctrl.SetBitmap(self.img.Scale(w, h).ConvertToBitmap())

    def on_resize(self, evt):
        self.update_bitmap()


class MainWindow(wx.Frame):
    def __init__(self):
        initial_size = wx.Size(width=480, height=480)
        super().__init__(None, wx.ID_ANY, title='Main Window Title', size=initial_size)
        self.panel = wx.Panel(self)
        self.image = ImagePanel(self.panel)

        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(self.image, proportion=1, flag=wx.EXPAND)
        self.panel.SetSizer(vbox)


if __name__ == '__main__':
    app = wx.App()
    win = MainWindow()
    win.Show()
    win.image.load_image()
    app.MainLoop()

The image will never be resized automatically. 图像将永远不会自动调整大小。 If you want this behaviour, you need to handle wxEVT_SIZE and do it yourself in its handler. 如果您想要这种行为,则需要处理wxEVT_SIZE并自己在其处理程序中进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM