使用wx python缩放具有帧大小的背景图像

时间:2018-11-08 18:05:42

标签: python background panel wxwidgets frame

我一直在尝试最终拥有一个使用简单矢量图像作为背景的应用程序,因此可以根据需要缩放和调整屏幕大小。我一直在用光栅照片对此进行测试,并尝试通过以下链接对某些内容进行修补:

https://www.blog.pythonlibrary.org/2010/03/18/wxpython-putting-a-background-image-on-a-panel/ How to resize and draw an image using wxpython? wxPython Background image on frame

不幸的是,我有点笨拙,尽管我了解正在发生的事情,但是我没有足够的经验来为自己的目的对其进行修改。话虽如此,我该如何在面板上保留背景图像并使用wxpython实时按面板大小缩放图像?

我现在有一些已拼凑的代码:

import pathlib
import wx

class MainApp(wx.App):

    def __init__(self, redirect=False, filename=None):

        wx.App.__init__(self, redirect, filename)
        dlg = MainFrame(parent=None,title="IvyVine")
        dlg.Show()

####################################

class MainFrame(wx.Frame):

    def __init__(self, parent, title):

        wx.Frame.__init__(self, parent=None, title="IvyVine",size=(1000,500))
        panel = MainPanel(self)
        self.Center()

        self.Show(True)

    def OnExit(self,e):
        self.Close(True) #Closes the frame

####################################

class MainPanel(wx.Panel):

    def __init__(self, parent):

        bg_img = 'window.JPG'

        wx.Panel.__init__(self, parent=parent)
        self.SetBackgroundStyle(wx.BG_STYLE_ERASE)
        self.frame = parent
        self.bg = wx.Bitmap(bg_img)
        self._width, self._height = self.bg.GetSize()

        sizer = wx.BoxSizer(wx.VERTICAL)
        hSizer = wx.BoxSizer(wx.HORIZONTAL)

        for num in range(3):
            label = "Button %s" % num
            btn = wx.Button(self,label=label)
            sizer.Add(btn,0,wx.ALL,5)

        hSizer.Add((1,1), 1, wx.EXPAND)
        hSizer.Add(sizer, 0, wx.TOP, 100)
        hSizer.Add((1,1), 0, wx.ALL, 75)
        self.SetSizer(hSizer)
        self.Bind(wx.EVT_SIZE, self.OnSize)
        self.Bind(wx.EVT_PAINT, self.OnPaint)

    #---------------------------

    def scale_bg(self, bitmap, width, height):

        pass

    #---------------------------

    def OnSize(self, size):

        self.Layout()
        self.Refresh()

    #---------------------------

    def OnPaint(self, evt):

        dc = wx.BufferedPaintDC(self)
        self.Draw(dc)

    #---------------------------

    def Draw(self, dc):

        cliWidth, cliHeight = self.GetClientSize()
        if not cliWidth or not cliHeight:
            return
        dc.Clear()

        # The image I'm using is very large, and this math only captures a part of it for some reason.
        xPos = (cliWidth - self._width)/2
        yPos = (cliHeight - self._height)/2
        #img = self.scale_bg(self.bg)
        dc.DrawBitmap(self.bg, xPos, yPos)

#################################

if __name__ == "__main__":

    app = MainApp()
    app.MainLoop()

****编辑代码****

####################################

MainPanel类(wx.Panel):

def __init__(self, parent):

    bg_img = 'window.JPG'

    wx.Panel.__init__(self, parent=parent)
    self.SetBackgroundStyle(wx.BG_STYLE_ERASE)
    self.frame = parent
    self.bg = wx.Image(bg_img, wx.BITMAP_TYPE_ANY)
    #store sizes
    self.bgh = self.bg.GetHeight()
    self.bgw = self.bg.GetWidth()

    sizer = wx.BoxSizer(wx.VERTICAL)
    hSizer = wx.BoxSizer(wx.HORIZONTAL)

    for num in range(3):
        label = "Button %s" % num
        btn = wx.Button(self,label=label)
        sizer.Add(btn,0,wx.ALL,5)

    hSizer.Add((1,1), 1, wx.EXPAND)
    hSizer.Add(sizer, 0, wx.TOP, 100)
    hSizer.Add((1,1), 0, wx.ALL, 75)
    self.SetSizer(hSizer)
    self.Bind(wx.EVT_SIZE, self.OnSize)
    self.Bind(wx.EVT_PAINT, self.OnPaint)

#---------------------------

def OnSize(self, size):

    self.Layout()
    self.Refresh()

#---------------------------

def OnPaint(self, evt):

    dc = wx.BufferedPaintDC(self)
    self.Draw(dc)

#---------------------------

def Draw(self, dc):

    cliWidth, cliHeight = self.GetClientSize()
    if not cliWidth or not cliHeight:
        return
    dc.Clear()

    #calculate scale factors
    fw = cliWidth / float(self.bgw)
    fh = cliHeight / float(self.bgh)
    scaledimage = self.bg_img.Scale(fw, fh)

    dc.DrawBitmap(wx.Bitmap(scaledimage))

2 个答案:

答案 0 :(得分:0)

首先,请勿使用wx.Image,因为它无法缩放。使用wx.Image

def __init__(self, parent):

    bg_img = 'window.JPG'

    wx.Panel.__init__(self, parent=parent)
    self.SetBackgroundStyle(wx.BG_STYLE_ERASE)
    self.frame = parent
    self.bg = wx.Image(bg_img, wx.BITMAP_TYPE_ANY)
    #---store sizes---
    self.bgh = bg.GetHeight()
    self.bgw = bg.GetWidth()

下一步,在Draw函数中,缩放图像并绘制

def Draw(self, dc):

    cliWidth, cliHeight = self.GetClientSize()
    if not cliWidth or not cliHeight:
        return
    dc.Clear()

    # Calculate scale factors
    # The final image will be distorted. Change this maths for "fit in window", but leaving margins
    fw = cliWidth / float(self.bgw)
    fh = cliHeight / float(self.bgh)
    scaledimage = self.bg.Scale(fw, fh)

    #dc wants a Bitmap
    dc.DrawBitmap(wx.Bitmap(scaledimage))

答案 1 :(得分:0)

Image.Scale方法等待整数。应该给出图片的新尺寸,而不是比例。

另外,DC.DrawBitmap方法缺少面板中图像的位置作为输入参数(x = 0,y = 0)。

通过这些修复,Draw方法变为:

     def Draw(self, dc):
    
        cliWidth, cliHeight = self.GetClientSize()
        if not cliWidth or not cliHeight:
            return
        dc.Clear()
    
        #Scale to client size
        scaledimage = self.bg_img.Scale(cliWidth, cliHeight)
        dc.DrawBitmap(wx.Bitmap(scaledimage), 0, 0)
相关问题